如何在Tcl中创建动态变量名
我有一个超级列表,例如:如何在Tcl中创建动态变量名,tcl,Tcl,我有一个超级列表,例如: set superlist {{1 2 3} {4 5 6} {7 8 9} {10 11 12} ...} list1 {1 2 3} list2 {4 5 6} list3 {7 8 9} ... 但我事先不知道超级列表中会有多少子列表。是否仍然可以创建子列表,例如: set superlist {{1 2 3} {4 5 6} {7 8 9} {10 11 12} ...} list1 {1 2 3} list2 {4 5 6} list3 {7 8 9}
set superlist {{1 2 3} {4 5 6} {7 8 9} {10 11 12} ...}
list1 {1 2 3}
list2 {4 5 6}
list3 {7 8 9}
...
但我事先不知道超级列表中会有多少子列表。是否仍然可以创建子列表,例如:
set superlist {{1 2 3} {4 5 6} {7 8 9} {10 11 12} ...}
list1 {1 2 3}
list2 {4 5 6}
list3 {7 8 9}
...
我想我必须根据superlist中子列表的数量创建变量列表名。有人能帮我解决这个问题吗,即如何在执行代码时装箱变量名?您可以这样做:
但是不要!
实际上,使用数组几乎肯定会让您更快乐
foreach sublist $superlist {
set list([incr index]) $sublist
}
原因是使用变量索引访问的语法:
for {set index 1} {$index <= 3} {incr index} {
puts "at $index is the list $list($index)"
}
(这是读取名为variable BTW的变量的最佳方法。)先生,它工作得非常好,您详细说明答案的方式对像我这样的初学者非常有帮助。实际上需要这样做吗:为什么不使用
lindex$superlist 0
,lindex$superlist 1
。。。什么时候需要?