The $$var
doesn't do double substitution. It puts a $
in front of the contents of var
. That results in the literal expression $p.txt_bc
, which is syntactically invalid because .
isn't an operator in Tcl's expression language.
To do double substitution, you should use [set]
for the outer layer (and brace your expression so that Tcl can compile it, please), like this:
expr { [set $var] }
However, experience suggests that there are usually better approaches than using double substitution. In particular, most cases are better addressed by using either associative arrays or upvar
.
set var "p.txt_bc"
set data(p.txt_bc) 1
expr { $data($var) }
set var "p.txt_bc"
set p.txt_bc 1
upvar 0 $var v
expr { $v }
The version with upvar
is more common when the variable is in a different scope to the current one, and upvar 0
isn't really recommended at the global level as it isn't easy to undo (because most operations on variables work on the alias as if it was the target).