More About Array Names In String Variables 
Author Message
 More About Array Names In String Variables

You have a variable Arr that contains the name of an array X.  How do
you get at array values such as X(1) using the variable Arr?

  Here are various example solutions for the case in which you want to
use the array element as a lvalue, i.e. the case in which you normally
write "X(1)".

1) set [set Arr](1) value
2) set ${Arr}(1) value
3) set $Arr\(1) value
4) array set $Arr {1 value}
5) setArray $Arr 1 value

All of these examples assign "value" to the array element at index "1".

  I don't think solution 4 is available in versions of Tcl before 7.4.
Be aware that the "array set" function is actually more general in that
it can deal with multiple values.

  Solution 5 is implemented below.

  Here are various example solutions for the case in which you want to
use the array element as an rvalue, i.e. the case in which you normally
write "$X(1)".

1) [set [set Arr](1)]
2) [subst $${Arr}(1)]
3) [set $Arr\(1)]
4) [array get $Arr 1]
5) [setArray $Arr 1]

All of these expressions return the value of the array X at index 1
through command substitution.

  There are other variations on the first three examples which replace
the outer set command with the $ and subst combination, and vice versa.

  I don't think solution 4 is available in versions of Tcl before 7.4.
Instead of returning the value of the array at the specified index, it
returns a two element list consisting of both the index and the value.
Also, like "array set", it can deal with multiple array values.
Finally, the index can be a glob pattern.  Short said, it's a fine
function that isn't a real good fit in this particular context.

  Solution 5 is implemented here.  The setArray procedure works
analogously to the set command except it has an extra parameter for an
array index.  Another example of its use is

setArray X $Index {another value}

which has the same effect as

set X($Index) {another value}

  In other words, setArray is a version of set for arrays that works
when the array name is given either directly or indirectly (through
variable substitution).

  I rather like setArray which is why I created it.  Here is the
implementation.

proc setArray {VariableName Index {NewValue ""}} {
  ## analogous to set; for accessing array values when array name is a variable
  ## by J Adrian Zimmer ( http://www.*-*-*.com/ )
   if {$NewValue=="" } {
      uplevel return \[set [set VariableName]([set Index])\]
   } else {
      uplevel return \[set [set VariableName]([set Index]) [set NewValue]\]
   }

Quote:
}

                         ...........................

    The next virtual class for my Internet Introduction to Tcl/Tk begins
    on Sept 2.  See http://www.*-*-*.com/


Sat, 13 Feb 1999 03:00:00 GMT  
 More About Array Names In String Variables

Quote:

> You have a variable Arr that contains the name of an array X.  How do
> you get at array values such as X(1) using the variable Arr?

Take a look at the upvar command.  Example:

upvar #[info level] $Arr larray
set larray(1) value

**********************************************************************
* Gerald W. Lester                       | Voice:  +1 (504)-889-2784 *
* Computerized Processes Unlimited, Inc. | FAX:    +1 (504)-889-2799 *

* Metairie, LA 70001                     | WWW: http://www.cpu.com   *
*                  "Consulting System Integrators"                   *
**********************************************************************



Sat, 13 Feb 1999 03:00:00 GMT  
 More About Array Names In String Variables


Quote:
>You have a variable Arr that contains the name of an array X.  How do
>you get at array values such as X(1) using the variable Arr?
>  Here are various example solutions for the case in which you want to
>use the array element as a lvalue, i.e. the case in which you normally
>write "X(1)".
>1) set [set Arr](1) value
>2) set ${Arr}(1) value
>3) set $Arr\(1) value
>4) array set $Arr {1 value}
>5) setArray $Arr 1 value

You're leaving a very important one out:

        upvar 0 $Arr Z

then you can get to X(1) using Z(1), e.g.:

        set Z(1) value
and
        $Z(1)

--

 ======================================================================
"Folk, if you are not checking the Frequently Asked Questions documents
before you ask questions, you are taking longer to resolve problems,
and annoying more folk, than necessary."   -- Larry W. Virden



Sun, 14 Feb 1999 03:00:00 GMT  
 More About Array Names In String Variables


Quote:
>Take a look at the upvar command.  Example:

>upvar #[info level] $Arr larray
>set larray(1) value

You can also use 0 instead of #[info level]. So it simplifies even further:

upvar 0 $Arr larray
set larray(1) value



Wed, 17 Feb 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. array name in variable variable

2. how to access arrays with variable for array names and index

3. access to array if name of array is variable

4. HOWTO: reference elements of an array if array name is in a variable

5. Using a variable name for a variable name??

6. Array to String and String to Array

7. Procedure name in a string variable

8. labview 7 index waveform array by channel name string

9. Using variable names as strings

10. treating variable names as strings (NEWBIE)

11. String to Variable Name Mapping

12. Variable names in text strings

 

 
Powered by phpBB® Forum Software