TCL 中带有括号和大括号的双美元是什么意思? [英] What does a double dollar with parentheses and braces following mean in TCL?
问题描述
下面这行有什么作用?
eval "set abc \$${SID}(abc)"
我的尝试
我知道 $someArray(index)
是在 TCL 中访问数组的方式.所以
My try
I know that $someArray(index)
is how arrays are accessed in TCL.
So
set abc $SID(abc)
希望 abc
成为数组 SID
的键(据我所知,abc
可以是 TCL 中的所有数组是关联的.这是正确的吗?).然后将值赋给 abc
.
would expect abc
to be a key of the array SID
(as far as I know abc
could be anything as all arrays in TCL are associative. Is this correct?). Then it assigns the value to abc
.
所以在伪代码中该行是
So in Pseudocode that line does
Ensure: SID is array, abc exists
abc <- SID[abc]
但是为什么 SID
周围有大括号?为什么是 eval
?
But why are there braces around SID
? Why the eval
?
推荐答案
大括号用于防止替换超过必要字符数的变量.
Braces are used to prevent substitution of variables of more characters than necessary.
$variableonetwo
引用名为variableonetwo
的变量并返回其值:
Refers to a variable named variableonetwo
and returns its value:
% set variableonetwo 1
% puts $variableonetwo
1
但是……
${variableone}two
指名为variableone
的变量和字符串two
:
Refers to the variable named variableone
and the string two
:
% set variableone 1
% puts ${variableone}two
1two
% puts $variableonetwo
can't read "variableonetwo": no such variable
<小时>
eval
用于...很好地评估/执行命令.
eval
is used to... well evaluate/execute a command.
eval "set abc \$${SID}(abc)"
如果 $SID
的值为 foo
是一个数组,则将尝试评估以下内容.
Will try to evaluate the following if $SID
has the value foo
which is an array.
set abc $foo(abc)
在此处使用 eval
的一个优点是,您可以在同一行中使用 两个 替换,第一个替换 $SID
到数组名称,第二个来自数组值(来自提供的键).
An advantage of using eval
here is that you are able to have not one but two substitutions in the same line, the first being substituting $SID
to an array name and the second being from the array value (from the provided key).
这篇关于TCL 中带有括号和大括号的双美元是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!