Version 3 of how do i do $$var - double dereferencing a variable

Updated 2003-03-27 16:09:32

This issue of dereferencing seems to come up frequently enough to deserve a page of tips, suggestions, warnings, etc.

The basic situation is that a user is trying, either intentionally or accidentally, to do pointer like design.

So they end up discovering they need to do something like:

 set a "123"
 set b "3.1415"
 set c "The price of petrol in Peroria"

 foreach var {a b c} {
   puts [set $var]
 }

TV It's probably worth a page by itself as a subject. A pointer in C language sense is not the same as a indirect reference. Also, associations are not the same as referencing something by name and that is not the same as a pointer in C, in normal language or in for instance a URL. And a function, as clearly the case in tcl is referenced by name, and can be stored as a list.

The simple answer I guess is to use eval:

 set whichvar b
 set b 11
 eval puts $$whichvar

Which leaves a lot of questions about quoting, and preventing the evaluator to perform substitutions (possible through escaping).


Category Tutorial