TCL-将多个变量设置为0

TCL-将多个变量设置为0,tcl,var,Tcl,Var,我有很多计数变量,例如:count1 count2 count3 set count1 0 set count2 0 set count3 0 在TCL中是否有一种更短的方法来设置所有的count1…count100 0,而不是在单独的行中键入all ie:set count1[list…]如果有这么多的变量密切相关,我建议使用数组,您可以使用循环: for {set i 0} {$i <= 100} {incr i} { set count($i) 0 } 在上面的示例中,l

我有很多计数变量,例如:count1 count2 count3

set count1 0
set count2 0
set count3 0
在TCL中是否有一种更短的方法来设置所有的count1…count100 0,而不是在单独的行中键入all


ie:set count1[list…]

如果有这么多的变量密切相关,我建议使用数组,您可以使用循环:

for {set i 0} {$i <= 100} {incr i} {
    set count($i) 0
}
在上面的示例中,lrepeat将创建一个包含元素0的列表4次

我有很多计数变量,例如:count1 count2 count3

set count1 0
set count2 0
set count3 0
不要,只需维护一个Tcl列表,并按列表位置访问各种计数:

set count [list 0 0 0 0]; # This is your "multi-set"

lindex $count 0; # a.k.a. $count0 or [set count0]
lset count 0 5; # a.k.a. [set count0 5]

lindex $count 1; # a.k.a. $count1
lset count 1 10; # a.k.a. [set count1 10]
如果您仍然想将计数的列表编码分解为一组专用变量,这是Jerry建议的一个通用变体,使用lassign then:

set命令返回加载到变量中的值。因此,在将几个变量初始化为0时,可以执行以下操作:

set count1 [set count2 [set count3 0]]
但是有100个变量是不实际的


如果您有100个计数器,那么使用阵列几乎肯定会容易得多。计数器听起来像是在变量上使用incr命令来计数。由于Tcl 8.5,包含数组元素的变量不需要初始化为0,incr才能工作。您可以只使用incr count$x而不进行任何初始化。

从列表开始时,最好使用单个脚本for循环来定义变量,而不是使用lmap/lassign。如果您使用的是“压缩”数字索引,如在经典C或Fortran数组中,请使用列表。做那份工作要快得多。
% set varNames [lmap idx [lsearch -all $count *] {string cat count $idx}]
count0 count1 count2 count3
% lassign $count {*}$varNames
% info vars count*
count count0 count1 count2 count3
set count1 [set count2 [set count3 0]]