TCL 阵列密钥无法识别
可能的重复:
tcl 数组问题 - 带引号的键
我有以下代码:
set my_list1 {"a" "b"}
set my_list2 {"@1" "@2"}
array set my_array {}
foreach li1 $my_list1 li2 $my_list2 {
set my_array($li1) $li2
}
puts $my_array("a")
在列表中我收到错误“无法读取 my_array("a"):数组中没有这样的元素”
为什么?
我有它,因为当我写入时
set newVar "a"
puts $my_array($newVar)
它会返回值!
Possible Duplicate:
tcl array question - key with quotes
I have the following code:
set my_list1 {"a" "b"}
set my_list2 {"@1" "@2"}
array set my_array {}
foreach li1 $my_list1 li2 $my_list2 {
set my_array($li1) $li2
}
puts $my_array("a")
On the list line I get ERROR "can't read my_array("a"): no such element in array"
Why?
I have it, because when I write
set newVar "a"
puts $my_array($newVar)
it returns the value!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这只是 Tcl 中的其中之一。数组元素不是
my_array("a")
- 它是my_array(a)
。引用数组时不要包含引号。它们实际上不是必需的,尽管在这种情况下注意有害,当您首先将数据安装到阵列中时 - 即,就可以了。
Tcl 看起来很像一种“普通”编程语言,因此很容易忘记它的解析器实际上是多么原始。请记住,所有内容都被空格分解为“单词”。如果双引号字符前面没有空格,则它不在单词的开头,并且不再具有任何特殊意义。对数组元素的引用是一个单词,在变量插值之后,它必须具有完全正确的文本。您不能在元素名称两边加上引号,因为这些引号不是该单词的正确文本的一部分。
This is just one of those things in Tcl. The array element is not
my_array("a")
-- it'smy_array(a)
. Don't include the quotes when referencing the array. They're actually not necessary, although in that case note harmful, when you're installing the data into the array in the first place -- i.e.,would be just fine.
Tcl looks enough like a "normal" programming language that it's easy to forget how primitive its parser really is. Remember that everything is broken down into "words" by whitespace. If a double-quote character isn't preceded by whitespace, it isn't at the start of a word, and it no longer has any special significance. A reference to an array element is a single word, and after variable interpolation, it has to have exactly the right text. You can't put quote marks around the element name because simply those quote marks are not part of the correct text of that word.