TCL — referencing "variable" variables

Homepage Clovertech Forums Read Only Archives Cloverleaf Cloverleaf TCL — referencing "variable" variables

  • Creator
    Topic
  • #48487
    Todd Lundstedt
    Participant

    I am try to reference a variable variable name.. that is, the varName is not always the same, but it needs to be referenced by $varName.  Specifically, array names.

    So for the following code, I have a varName set of “threadList”, and it is a list equal to “thread1 thread2 thread3”

    I have two arrays defined, each one has 5 identically named elements.  The array names are thread1, and thread3

    tcl> array names thread1

    recBSnd recNSnd recTime recBRec recNRec recSite

    tcl> array names thread3

    recBSnd recNSnd recTime recBRec recNRec recSite

    tcl> puts $threadList

    thread1 thread2 thread3

    Code:

    foreach thrd $threadList {
     if {[info exists $thrd]} {
       append $sep $thrd(recBSnd) $sep $thrd(recBRec)
     }
    }

    My problem is arrays have to be referenced via the array name.  The array name (for the first run through) is “thread1”.

    puts $thread1(recBSnd)

    returns a value.  But I don’t know how to reference the array name by using a variable name.

    Any help would be appreciated.

    Thanks

    Todd

Viewing 2 reply threads
  • Author
    Replies
    • #58757
      Jim Kosloskey
      Participant

      Todd,

      Have you tried subst?

      Such as (assuming recBsnd has a value of 1)

      echo [array get [subst $thrd] recBsnd]

      which will return the key and value (as a list) – recBsnd 1

      or if you just want the value:

      echo [lindex [array get [subst $thrd] recBsnd

      which will return 1 from the get list

      Jim Kosloskey

      email: jim.kosloskey@jim-kosloskey.com 29+ years Cloverleaf, 59 years IT - old fart.

    • #58758
      Todd Lundstedt
      Participant

      Thanks Jim,

      I should have thought of the subst command.  It’s been a while since I have done TCL coding from scratch.

      Thanks again.

      Todd

    • #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

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

Forum Statistics

Registered Users
5,117
Forums
28
Topics
9,293
Replies
34,435
Topic Tags
286
Empty Topic Tags
10