I am getting an error invalid character "." while running following code in tcl shell :
set var "p.txt_bc"
set p.txt_bc 1
expr $$var
invalid character "." in expression "$p.txt_bc"
How to handle the "." character here so that the output is 1 ? I know I can replace the "." with something else to somehow make it work but is there a way to get the expected output without any substitution done to "." ?
The
$$vardoesn't do double substitution. It puts a$in front of the contents ofvar. 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: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.The version with
upvaris more common when the variable is in a different scope to the current one, andupvar 0isn'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).