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

TCL – How to read a nested variable from inside a procedure

I am trying to do the following:

set myvar_key something
set a myvar
proc test {} {
  # I am expecting mylocal_var to be "something", but it errors
  set mylocal_var [set ${::a}_key]
}

# The proc is called like this
test

Thanks in advance,
Pedro

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 :

You’re almost there, but just missed something. As your code already shows, a is in the global namespace, thus you need ::a.
Same is true for myvar_key, thus you need to do

set myvar_key something
set a myvar
proc test {} {
  set mylocal_var [set ::${::a}_key]
  puts $mylocal_var
}
test

prints "something"

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