如何使用三元运算符向 tcl 中的匿名字符串添加可选部分? [英] How do I use the ternary operator to add an optional part to an anonymous string in tcl?
问题描述
我是 tcl 的新手(对不起,如果答案很明显,但阅读教程和文档没有帮助).我在 tcl 中有一个声明:
I am new to tcl (sorry if the answer is obvious, but reading tutorials and documentation did not help). I have a statement in tcl that says:
startupitem.start "foo
\tbar"
我想做的是让foo"部分成为可选的,这取决于结果[variant_isset "alice"]
使用三元运算符而不使用变量.
What I would like to do is have the "foo" part become optional, depending on the outcome of
[variant_isset "alice"]
using the ternary operator and without using variables.
我已经尝试了几件事
startupitem.start "[variant_isset """alice"""?"""foo\n\t""":""""""] bar"
(当然有各种转义和组合或在双引号内使用双引号)但我没有成功.
(of course with all kinds of escapes and combo's or the use of the double quotes inside the double quotes) but I haven't succeeded.
如果 variant_isset
表达式返回 true 的结果是它等价于
The outcome if the variant_isset
expression returns true is that it is equivalent to
startupitem.start "bar"
推荐答案
您想查看 Tcl 的 expr
命令,它引入了 Tcl 的表达式子语言 incl.你称之为三元"运算符?
:
You want to check out Tcl's expr
command, which introduces Tcl's expression sub-language incl. what you call the "ternary" operator ?
:
startupitem.start "[expr {[variant_isset "alice"] ? "foo\n\t" : ""}]bar"
如果您碰巧最近使用了 Tcl,您可能更喜欢使用 string cat
,而不是就地:
If you happen to use a Tcl recent enough, you may want to prefer an outplace string assembly using string cat
, rather than inplace:
string cat [expr {[variant_isset "alice"] ? "foo\n\t" : ""}] "bar"
这篇关于如何使用三元运算符向 tcl 中的匿名字符串添加可选部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!