how to access arrays with variable for array names and index 
Author Message
 how to access arrays with variable for array names and index

I try to access arrays with variables for array name and array index, but
it doesn't work:

set arr_name MyName
puts $arr_name
# ---> MyName

set ${arr_name}(1,1) 11
puts $MyName(1,1)
# ---> 11

set index_name iname
set ${arr_name}($index_name,1) 22
puts $MyName($index_name,1)
# ---> 22 !!!!

#but:
puts ${arr_name}($index_name,1)
# ---> MyName(iname,1) ?????

what to do ?

thanks



Tue, 02 Nov 2004 02:53:34 GMT  
 how to access arrays with variable for array names and index

Quote:
> set index_name iname
> set ${arr_name}($index_name,1) 22
> puts $MyName($index_name,1)
> # ---> 22 !!!!

> #but:
> puts ${arr_name}($index_name,1)
> # ---> MyName(iname,1) ?????

> what to do?

How about this?

set ${arr_name}(${index_name},1)

Jeff



Tue, 02 Nov 2004 03:12:28 GMT  
 how to access arrays with variable for array names and index

Quote:
> set index_name iname
> set ${arr_name}($index_name,1) 22
> puts $MyName($index_name,1)
> # ---> 22 !!!!

> #but:
> puts ${arr_name}($index_name,1)
> # ---> MyName(iname,1) ?????

> what to do?

How about this?

set ${arr_name}(${index_name},1)

Jeff



Tue, 02 Nov 2004 03:12:59 GMT  
 how to access arrays with variable for array names and index

Quote:



>> set index_name iname
>> set ${arr_name}($index_name,1) 22
>> puts $MyName($index_name,1)
>> # ---> 22 !!!!

>> #but:
>> puts ${arr_name}($index_name,1)
>> # ---> MyName(iname,1) ?????

>> what to do?

> How about this?

> set ${arr_name}(${index_name},1)

> Jeff

unfortunately i can not access the array this way:

puts ${arr_name}(${index_name},1)
# ---> MyName(iname,1) ?????

Uwe



Tue, 02 Nov 2004 04:16:30 GMT  
 how to access arrays with variable for array names and index

Quote:



>> set index_name iname
>> set ${arr_name}($index_name,1) 22
>> puts $MyName($index_name,1)
>> # ---> 22 !!!!

>> #but:
>> puts ${arr_name}($index_name,1)
>> # ---> MyName(iname,1) ?????

>> what to do?

> How about this?

> set ${arr_name}(${index_name},1)

> Jeff

setting the array works also with

        ${arr_name}($index_name,1)

but unfortunately i can not access the array this way:

        puts ${arr_name}($index_name,1)
        puts ${arr_name}(${index_name},1)

the output is: "MyName(iname,1)" and not "22" like i wish

thanks for your support

Uwe



Tue, 02 Nov 2004 04:27:36 GMT  
 how to access arrays with variable for array names and index


Quote:

> > How about this?

> > set ${arr_name}(${index_name},1)

> > Jeff

> unfortunately i can not access the array this way:

> puts ${arr_name}(${index_name},1)
> # ---> MyName(iname,1) ?????

I think you misunderstood.  Notice the use of [set] in my example.  Try
this:

puts [set ${arr_name}(${index_name},1)]

Jeff



Tue, 02 Nov 2004 04:31:17 GMT  
 how to access arrays with variable for array names and index


Quote:

> > How about this?

> > set ${arr_name}(${index_name},1)

> > Jeff

> unfortunately i can not access the array this way:

> puts ${arr_name}(${index_name},1)
> # ---> MyName(iname,1) ?????

I think you misunderstood.  Notice the use of [set] in my example.  Try
this:

puts [set ${arr_name}(${index_name},1)]

Jeff



Tue, 02 Nov 2004 04:32:01 GMT  
 how to access arrays with variable for array names and index
Note that the Tcl parser examines each character *exactly* once.  So, in
you command
    puts ${arr_name}($index_name,1)
$arr_name is substituted with "MyName" and $index_name gets "iname",
resulting in
    puts MyName(iname,1)
There is no "double dereferencing"

You will want
    puts [set ${arr_name}($index_name,1)]
to dereference the variable MyName(iname,1)

Refer to
    http://www.tcl.tk/man/tcl8.3/TclCmd/Tcl.htm, particularly rule 10.
    http://mini.net/tcl/set

On Thu, 16 May 2002 20:53:34 +0200, Uwe Viergutz

Quote:

>I try to access arrays with variables for array name and array index, but
>it doesn't work:

>set arr_name MyName
>puts $arr_name
># ---> MyName

>set ${arr_name}(1,1) 11
>puts $MyName(1,1)
># ---> 11

>set index_name iname
>set ${arr_name}($index_name,1) 22
>puts $MyName($index_name,1)
># ---> 22 !!!!

>#but:
>puts ${arr_name}($index_name,1)
># ---> MyName(iname,1) ?????

>what to do ?

>thanks

--
Glenn Jackman



Tue, 02 Nov 2004 04:37:31 GMT  
 how to access arrays with variable for array names and index

Quote:

> I try to access arrays with variables for array name and array index, but
> it doesn't work:

> set arr_name MyName
> puts $arr_name
> # ---> MyName

> set ${arr_name}(1,1) 11
> puts $MyName(1,1)
> # ---> 11

> set index_name iname
> set ${arr_name}($index_name,1) 22
> puts $MyName($index_name,1)
> # ---> 22 !!!!

> #but:
> puts ${arr_name}($index_name,1)
> # ---> MyName(iname,1) ?????

> what to do ?

> thanks

Maybe, use upvar 0?

Like:

   upvar 0 MyName arr_name

it can be used "dynamically" as well, of course.

Regards,

Arjen



Tue, 02 Nov 2004 14:27:47 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. access to array if name of array is variable

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. Index Values of an Array inside a Cluster inside an Array

6. array as index to array

7. labview 7 index waveform array by channel name string

8. finding name/index of checkbutton array in -command

9. array with quoted index name

10. Variable in Array Index?

11. logical variables as array indexing

12. array name ARRAY regexp

 

 
Powered by phpBB® Forum Software