Re: Parse a Tcl variable name inside a string



On 31 okt, 13:46, pma...@xxxxxxxxx wrote:
Hi,
I would like to parse variable names from a string.
Let's assume I have the following variable set
set myvar {text_$var1_text_$var2}

The $var1 and $var2 must not be replaced immediately by their values.
Now if I do the following
set var1 12
set var2 25
puts [subst $myvar]

I get "text_12_text_25", this is what I expect.
Sometimes, var1 might not be set before the "subst $myvar" command,
leading to a crash.
I would like to parse the variable myvar before trying to subst it, in
order to find out that it requires the variables var1 and var2 to be
defined, and then check var1 and var2.
Now, my problems : the names "var1" and "var2" can change, and even
the number of sub-variables.
Moreover, the syntax $var or ${var} might be used.

I think I could use regexps, but how can handle the changing number of
matches ?
Thanks for your help
Philippe

How about parsing the error message instead?

catch {subst {$x}} msg

The message contains the exact name of the variable in
double quotes. Repeat that until all are set.

(I have used that on the Wiki somewhere, just can't
remember on which page - or perhaps it was in a different
program)

Regards,

Arjen

.



Relevant Pages