access to array if name of array is variable 
Author Message
 access to array if name of array is variable

in the following script there is an array of lists and a ref to this
array:

% set menuFile(0) {New 0 "Ctrl+N" null}
% set menuFile(1) {Open... 0 "Ctrl+O" null}
% set menuFile(2) {Close 0 "Ctrl+W" null}

% set ref menuFile              ;# reference to array

how can I access to the list elements in the array using the reference?

% lindex $menuFile(0) 0
New
--> no problem

but
% eval lindex $$ref(0) 0
can't read "ref(0)": variable isn't array

How can I force tcl to evaluate "$ref" before "(0)"

One solution is:
% eval lindex [format "$%s(0)" $ref] 0
New

I think there is a more elegant solution.

any ideas?

thanks
thomas
--

 *******************************************************
 Thomas Gusset,    Altendorferstr. 16,        9470 BUCHS
 phone: ++41 81 756 33 17  

 *******************************************************



Sun, 10 May 1998 03:00:00 GMT  
 access to array if name of array is variable

Quote:
>in the following script there is an array of lists and a ref to this array:

        ... snip ...

usually you would use 'upvar' in that example, like 'upvar #0 $ref myarry',
but you could also do something like 'set [set ref](0)'.
--
     Jeffrey Hobbs                           Office: 541/346-3998

                URL: http://www.cs.uoregon.edu/~jhobbs/



Sun, 10 May 1998 03:00:00 GMT  
 access to array if name of array is variable
: in the following script there is an array of lists and a ref to this
: array:

: % set menuFile(0) {New 0 "Ctrl+N" null}
: % set menuFile(1) {Open... 0 "Ctrl+O" null}
: % set menuFile(2) {Close 0 "Ctrl+W" null}

: % set ref menuFile            ;# reference to array

: how can I access to the list elements in the array using the reference?

: % lindex $menuFile(0) 0
: New
: --> no problem

: but
: % eval lindex $$ref(0) 0
: can't read "ref(0)": variable isn't array

: How can I force tcl to evaluate "$ref" before "(0)"

% eval lindex $${ref}(0) 0
New

Hugo van der Sanden



Mon, 11 May 1998 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

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

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

3. array name in variable variable

4. accessing to a variable by ref (variable is an array)

5. array name ARRAY regexp

6. Access Array Elements by Arrays Reference

7. Array Access and Naming

8. Variable Size arrays Was: Indeterminate arrays??

9. array command and non-existent variables/arrays

10. More About Array Names In String Variables

11. Q: Variables in associative array names

12. Array variable name decomposition - kludge leads to bug

 

 
Powered by phpBB® Forum Software