Reply To: TCL — referencing "variable" variables

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf TCL — referencing "variable" variables Reply To: TCL — referencing "variable" variables

#58759
Dick Bly
Participant

    dont forget cmds like  set and eval to perform functions on accessing variables for example you know that set x [set y] would be the equiv of set x $y but you can also do  set z 20;set y z ; set x [set $y]. now x has the vaue of 20.

    eval comes in handy when you need to do things like

    set x ${$name}(z)

    for example

    tcl>set ary(x) 3

    tcl>set y ary

    tcl>echo ${$y}(x) ;# attempt to use $y as an array name

    Error: can’t read “$y”: no such varibale

    tcl>

    tcl>echo $[set y](x) an attemt to echo the value

    $ary(x)

    an eval completes the reference

    tcl>eval echo $[set y](x)

    3

    tcl>

    and then you could do

    tcl>echo $$y(x)

    Error: can’t read “y(x)”: variable isn’t array

    tcl>echo $${y}(x)

    $ary(x)

    which then leads to

    tcl>eval echo $${y}(x)

    3