TCL阵列的问题 - 关键与报价 [英] tcl array question - key with quotes
问题描述
这让我感到惊讶。
>set foo("bar") 12
12
>parray foo
foo("bar") = 12
>set foo(bar) 12
12
>parray foo
foo("bar") = 12
foo(bar) = 12
似乎字面foo是不一样的foo的。然而
it seems that the literal foo is not the same as "foo". And yet
>string length foo
3
>string length "foo"
3
我是什么没能理解。
what am I failing to understand
推荐答案
的字符只有一个单词的开头(或末尾的特殊到Tcl的解析器这是由
开始一个字
,当然)。事实上,如果你会把空格,你会得到一个错误:
The "
character is only special to Tcl's parser at the start of a word (or the end of a word that was started by "
, of course). In fact, if you'd put spaces in you would have got an error:
% set foo("b a r") 2
wrong # args: should be "set varName ?newValue?"
在那里你做了字符串长度
要求的情况下,的是。EM>在单词所以它是特殊的开始。如果我们把一个额外领先的垃圾字符,我们看到的
消失的特殊性:
In the case where you're doing the string length
calls, the "
is at the start of a word so it is special. If we put an extra leading junk character, we see that the specialness of "
goes away:
% string length x"bar"
6
如果你正在做一些与数组索引复杂的,我觉得它通常是更容易把一个变量本身的元素的名称,自那时以来,它更清晰这是怎么回事(通常更容易调试过):
If you're doing something complicated with array indices, I think it's usually easier to put the name of the element in a variable itself, since then it's clearer what's going on (and usually easier to debug too):
set idx "bar"
set foo($idx) 12
这篇关于TCL阵列的问题 - 关键与报价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!