TCL 读取数量动态增加的变量

问题描述 投票:0回答:1

我们在 foreach 循环中创建动态变量,如下所示:

set res01_ipsladata [split [exec "show ip sla history"] \n]
set res01_lineindicator 0
foreach line $res01_ipsladata {
    if {[llength $line] >= 1} {
        incr res01_lineindicator
        set res01_hop$res01_lineindicator [lindex $line 3]
        set res01_rtt$res01_lineindicator [lindex $line 5]
    }
}

所以基本上我们在这次运行之后有一堆变量,如下所示:

res01_hop1
res01_hop2
res01_hop3
res01_hop...

所以你明白了。现在我想将这些变量一一输出:

for {set a 1} {$a <= $highesthopcount} {incr a} {
    puts "showing variable $res01_hop$a"
}

所以基本上我希望循环能够得到所有变量 res01_hop1 到 max 的输出。但我得到了错误:

can't read "res01_hop": no such variable

那么我如何“合并”这些变量,以便将它们作为一个变量读取?

for-loop variables tcl
1个回答
0
投票

不客气:

set a 1
set res01_hop$a abc
puts "showing variable [set res01_hop$a]"
© www.soinside.com 2019 - 2024. All rights reserved.