Search code examples
tcldereference

Tcl dereferencing: How to dereference a variable inside a string


I have following situation:

% set a 20
20
% set in_word "Value of a is \$a"
Value of a is $a

Now, I would like to print (or set this string to another variable), Value of a is 20 by operating on the variable in_word.

The similar (possibly the simpler case) of dereferecning is covered in an essay which is also linked in Tclers wik here. The case covered there is of this nature:

% set x y
y
% set y 20
20
% set [set x]
20
%

This case parallels with my question in that I have a string with a variable inside it, instead of a direct variable. I am not sure how I can apply the idea in this case to my problem. I tried using eval but I did not get the effect I wanted.


Solution

  • The subst command is what you are looking for:

    puts [subst $in_word]