如何在tcl中添加列表元素

如何在tcl中添加列表元素,tcl,Tcl,我的清单如下: test = {a[2] r[5] f[6] t[8]} {d[32] g[66] k[88]} {w[2] e[33]} 测试列表的大小是可变的,可以有任意数量的元素 我希望总数计算为: 总计=4+3+2=9 我正在尝试类似的东西,但它给出了一个错误 set result "" set index1 "" foreach index1 [llength $test] { set value [llength [lindex $test $index1]]

我的清单如下:

test = {a[2] r[5] f[6] t[8]} {d[32] g[66] k[88]} {w[2] e[33]}
测试列表的大小是可变的,可以有任意数量的元素

我希望总数计算为:

总计=4+3+2=9

我正在尝试类似的东西,但它给出了一个错误

set result ""
set index1 ""  
foreach index1 [llength $test] {
        set value [llength [lindex $test $index1]]
        result1 = expr [$value + $result1]
        puts $result1
}
它给出了以下错误:

invalid command name "0"

谢谢。

要为列表中的每个成员做点什么,请始终使用
foreach
,而
incr
非常适合各种计数。在这种情况下:

set total 0;  # In case the input list is empty
foreach sublist $test {
    incr total [llength $sublist]
}
# The value you are looking for is in the “total” variable