string Tcl:如果变量为空,则默认?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10893959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tcl: default if variable is empty?
提问by DevSolar
I've "inherited" some Tcl code, and while I worked through some tutorials and can make sense out of the language, my own Tcl constructs lack a certain... finesse.
我已经“继承”了一些 Tcl 代码,虽然我完成了一些教程并且可以理解该语言,但我自己的 Tcl 构造缺乏某种......技巧。
For example, I have this code:
例如,我有这个代码:
puts "Column 'name': [ $queryRs getString name ]"
$queryRs
is the result set of a SQL query. The [ $queryRs getString name ]
construct retrieves the content of the table column "name" from the current row in the result set. If the database field is NULL, the puts
does not print anything.
$queryRs
是 SQL 查询的结果集。该[ $queryRs getString name ]
构造从结果集中的当前行检索表列“name”的内容。如果数据库字段为 NULL,puts
则不打印任何内容。
I would like to print a "default" string instead, i.e. if [ $queryRs getString name ]
results in nothing, I'd like to replace it with "--"
.
我想打印一个“默认”字符串,即如果[ $queryRs getString name ]
没有结果,我想用"--"
.
Now, I could do something like this:
现在,我可以做这样的事情:
set nameVar "[ $queryRs getString name ]"
if { [ string length $nameVar ] == 0 } {
set nameVar "--"
}
puts "Column 'name': $nameVar"
But there hasto be a more compact solution, something that can be done inline instead of adding four lines and a temporary variable. Help, please?
但是必须有一个更紧凑的解决方案,可以内联完成而不是添加四行和一个临时变量。请帮忙?
回答by kostix
Two things.
两件事情。
First, Tcl doesn't have a notion of NULL
(nil
, undefined
or whatever) value,and when you want to simulate such a value you have to either use a variable and test for its existenceor use an entry in an array
of dict
ionaryand test for its existence, too. If such a variable/entry exists, then the value is defined, otherwise it's not.
首先,TCL不具备的概念NULL
(nil
,undefined
或其他)值,而当你想模仿你有这样的值或者使用一个变量,它的存在测试或在使用中的条目array
的dict
ionary和测试其存在也是。如果存在这样的变量/条目,则定义该值,否则不定义。
Unfortunately, the creator of your inherited code apparently did not care about the case where a variable can be NULL
so NULLs
are indistinguishable from variables having default values (an empty string).
不幸的是,你的继承代码的创造者显然没有在意,其中一个变量可以是这样NULL
那么NULLs
是从具有默认值(一个空字符串)变量没有什么区别。
Next, you can use a helper procedure to do what you need:
接下来,您可以使用辅助程序来执行您需要的操作:
proc ValueOrDef {val {def --}} {
expr {$val ne "" ? $val : $def}
}
and then go like this:
然后像这样:
puts [ValueOrDef [$queryRs getString name]]
puts [ValueOrDef [$queryRs getString name] "some other default"]
回答by bmk
You could use the x?y:z
construct of the expr
command. x
is the condition, y
is the alternative if the condition is met and z
is the alternative if x
is not met.
您可以使用命令的x?y:z
构造expr
。x
是条件,y
如果满足条件,则为备选方案,如果不满足,z
则为备选方案x
。
E.g. (but still with a temporary variable):
例如(但仍然有一个临时变量):
set nameVar [ $queryRs getString name ]
puts "Column 'name': [expr {[string length $nameVar]>0 ? $nameVar : "--"}]"