Variable Name in Variable

Homepage Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Variable Name in Variable

  • Creator
    Topic
  • #50527
    John Zalesak
    Participant

    I am trying to get the value of a variable who’s name is in another variable.  As in I have the results of [info global] in a list and I want to get access to the global variables values where the name of the variables is in the list.

    foreach {i} [lsort [info global]] {

      echo $i is $$i

    }

    Only $$i does not return the value of the variable it returns the name of the variable with a $ in front of it.

    I have tried every combo of “” and {} i could think of to get it to work to no avail.

    This should be simple but can not find it anywhere.

    Thanks in advance.

Viewing 2 reply threads
  • Author
    Replies
    • #66479
      Charlie Bursell
      Participant

      You must not have taken my advanced Tcl class  🙂

      echo $i is [set $i]

    • #66480
      Charlie Bursell
      Participant

      I did not look at this closely enough.  I note you are trying to echo out all of the global values.  There are things to consider.

      1.  Unless you are execting in the global scope, e.g., hcitcl, you must either declare the name as global or use the full global namespace path like ::globname.

      2.  Some of the globals are arrays and will cause a Tcl error if not accessed as such

      3.  Simply becuse a name is global does not make it exist.  It must be set to something before it exists.  It would be a Tcl error to access it if it does not exist.

      So I offer up the following which will work in or out of the global scope, for example inside a proc.  You must be aware that to echo all of the global variables will generate a *LOT* of data

      foreach glob [info globals] {

         global $glob

         # Is it an array?

         if {[array exists $glob]} {

             echo nThe values of global array $glob are:

             parray $glob

             continue

         }

         # It is not an array, make sure it exists

         if {[info exists $glob]} {

             echo nThe value of global variable $glob is: [set $glob]

         } else {

             echo nThe value of global variable $glob is: UNDEFINED

         }

      }

    • #66481
      John Zalesak
      Participant

      Charlie,

      Thanks for your help.  This is exactly what I needed.  I did not even know I had an array issue because I could not get past the [set $glob] issue.

      At this point, I want to dump them all so I know what is there, how to get to them and what format the output is.

      Thanks again.

Viewing 2 reply threads
  • The forum ‘Cloverleaf’ is closed to new topics and replies.

Forum Statistics

Registered Users
5,041
Forums
28
Topics
9,199
Replies
34,021
Topic Tags
266