tclsubst

subst variable names with "point"


Let's say I have this code :

set a 1
set a.b a
set thing a.b
puts [subst $$thing]

The answer I would expect on the last line would be "a", but tcl answers 1.b I tried to put \ everywhere before the . but it didn't changed anything.

Is there away to get a from thing variable ?


Solution

  • Tcl does not double evaluate two consecutive dollar signs.

    The $thing characters in your command subst $$thing are first replaced by the value of $thing, which is a.b.

    Subsequently, the subst command is evaluated like this:

    subst $a.b
    

    The above subst command replaces $a with 1, which explains why you get 1.b returned.

    A reliable way to do multiple variable interpolation is with the set command without a second argument. Chain together multiple set commands to interpolate multiple times.

    puts [set thing]
      --> a.b
    puts [set [set thing]]
      --> a
    puts [set [set [set thing]]]
      --> 1