3 ms·
mea culpa on the eval/expr. I did the comments inline for brevity. I am confused about the $$ examples though, since swear I have worked on production code th
by bloaf 6y ago
mea culpa on the eval/expr. I did the comments inline for brevity.
I am confused about the $$ examples though, since swear I have worked on production code that literally behaves like that. Maybe they had some extra non-alphanumeric bits. This behaves the way I gave originally
set b a
set (a) 5
puts $($b)
Edit:
I found the production code. There was both
puts [expr 3 - $$b]
and
puts [subst $$b]
and the Steve example was actually two steps:
set nameVariable [subst var$$b]
puts [subst $$nameVariable]
- suyjuris 6y agoYeah, that is another dark corner. I was not quite precise (I forgot this behaviour existed): $ expansion does allow for parentheses as well, so that arrays work. Meaning $($b) is the same as [set ($b)] which is element $b of the array in variable {} (empty string; that is the name of the variable). The docs [1] say: > $name(index) > Name gives the name of an array variable and index gives the name of an element within that array. Name must contain only letters, digits, underscores, and namespace separators, and may be an empty string. [...] Command substitutions, variable substitutions, and backslash substitutions are performed on the characters of index. So that means: set b a set (a) a ;# this sets element a of the variable {} to a puts $($b) ;# prints that element info vars ;# all variables, note that {} is in there puts $($($($b))) ;# Of course this works... [1] http://www.tcl.tk/man/tcl8.6/TclCmd/Tcl.htm#M14 http://www.tcl.tk/man/tcl8.6/TclCmd/Tcl.htm#M14