Two alternatives are
set a 0.1
set b 20.0
set c {
{text1 text2 a text3}
{text4 text5 b text6}
}
foreach i $c {
set CheckVal [lindex $i 2]
puts "Threshold is [set $CheckVal]"
}
Store the variable name instead of the value, and get the value by a two-step substitution ($$Checkval
doesn't work, but [set $CheckVal]
does) in the puts
invocation.
set a 0.1
set b 20.0
set c {
{text1 text2 $a text3}
{text4 text5 $b text6}
}
foreach i $c {
set CheckVal [lindex $i 2]
puts [subst "Threshold is $CheckVal"]
}
This is double substitution rather than two-step substitution. It looks simple, but subst
is actually a bit tricky and is almost a last-resort technique.
Regardless of which solution you use, this kind of scheme where you store a reference to a variable value in a structure is fragile since it depends on the original variable being in scope and still existing when the reference is dereferenced. At the very least, you should store a qualified name (namespace and name) in your list. If the variable is a local variable, you need to use it during the same call as when it was stored (or from further up the call stack using upvar
, but don't go there).