Search code examples
for-loopvariablestcl

TCL read variables with a dynamically increasing number


We create dynamic variables in a foreach loop like this:

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]
    }
}

So basically we have a bunch of variables after this run that look like this:

res01_hop1
res01_hop2
res01_hop3
res01_hop...

So you get the idea. Now i want to output these variables one by one:

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

So basically i expect the loop to get me the output of all variables res01_hop1 to max. but instead i get the error:

can't read "res01_hop": no such variable

So how can i "merge" these tho variables so that they are getting read as a single one?


Solution

  • You'r welcome:

    set a 1
    set res01_hop$a abc
    puts "showing variable [set res01_hop$a]"