Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading