Q: Variables in associative array names 
Author Message
 Q: Variables in associative array names

The intention of the following code is to set an element in ten
associative arrays:

        for {set i 0} {$i <= 10} { incr i} {

                set array_$i(value) 987
        }

The result is the following error:

        can't read "i(value)": variable isn't array

Any suggestions?  TIA,

        john derose



Sun, 22 Nov 1998 03:00:00 GMT  
 Q: Variables in associative array names

Quote:

>The intention of the following code is to set an element in ten
>associative arrays:

>    for {set i 0} {$i <= 10} { incr i} {

>            set array_$i(value) 987
>    }

>The result is the following error:

>    can't read "i(value)": variable isn't array

>Any suggestions?  TIA,

See the FAQ

You need to do this instead

        set array_[set i](value) 987

--
- hops

Everything disclaimed (including disclaimer)

Mike Hopkirk ( hops )  |       Whenever possible steal code.
SCO Inc                |          Tom Duff. Bell Labs



Sun, 22 Nov 1998 03:00:00 GMT  
 Q: Variables in associative array names


Quote:

>    for {set i 0} {$i <= 10} { incr i} {
>            set array_$i(value) 987

replace the set line with:
        set [subst array_$i](value) 987

--
     Jeffrey Hobbs                           Office: 541/346-3998

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



Sun, 22 Nov 1998 03:00:00 GMT  
 Q: Variables in associative array names


Quote:

> The intention of the following code is to set an element in ten
> associative arrays:

>    for {set i 0} {$i <= 10} { incr i} {

>            set array_$i(value) 987
>    }

> The result is the following error:

>    can't read "i(value)": variable isn't array

Try one of:

    set array_${i}(value) 987
    set array_[set i](value) 987
    set [subst {array_$i}](value) 987

Donal.
--
Donal K. Fellows, (at work)       | Donal K. Fellows, (at home)
Dept. of Computer Science,        | 6, Randall Place, Heaton,
University of Manchester          | Bradford, BD9 4AE
U.K.      Tel: ++44-161-275-6137  | U.K.          Tel: ++44-1274-401017

--------------------------------------------------------------------------
          <http://r8h.cs.man.ac.uk:8000/>  for my home page



Mon, 23 Nov 1998 03:00:00 GMT  
 Q: Variables in associative array names

Quote:



>>        for {set i 0} {$i <= 10} { incr i} {
>>                set array_$i(value) 987
>replace the set line with:
>    set [subst array_$i](value) 987

Just a few other possibilities (there are plenty more):

        set array_${i}(value) 987

        set array_[set i](value) 987

        set array_[subst $i](value) 987

        set [subst array_\$i](value) 987

        set array_[subst {$i}](value) 987

        set [format array_%s(value) $i] 987

Tcl is quite versatile.

--

 ======================================================================
"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



Mon, 23 Nov 1998 03:00:00 GMT  
 Q: Variables in associative array names

Quote:

> The intention of the following code is to set an element in ten
> associative arrays:

>         for {set i 0} {$i <= 10} { incr i} {

>                 set array_$i(value) 987
>         }

> The result is the following error:

>         can't read "i(value)": variable isn't array

This works, but it seems like it should be possible in one statement:

         for {set i 0} {$i <= 10} { incr i} {
                 set a array_$i
                 set ${a}(value) 987
         }

--
...RickM...



Mon, 23 Nov 1998 03:00:00 GMT  
 Q: Variables in associative array names

Quote:

>The intention of the following code is to set an element in ten
>associative arrays:

>    for {set i 0} {$i <= 10} { incr i} {

>            set array_$i(value) 987
>    }

You've got to escape the open bracket so that the variable name
goes in a literal text

Try the following:-

        for {set i 0} {$i <= 10} { incr i} {

                set array_$i\(value) 987
        }



Tue, 24 Nov 1998 03:00:00 GMT  
 Q: Variables in associative array names

Quote:

>The intention of the following code is to set an element in ten
>associative arrays:

>    for {set i 0} {$i <= 10} { incr i} {

>            set array_$i(value) 987
>    }

>The result is the following error:

>    can't read "i(value)": variable isn't array

>Any suggestions?  TIA,

>    john derose

Try:

set array_${i}(value) 987
OR:
set array_[set i](value) 987

/Flemming



Tue, 24 Nov 1998 03:00:00 GMT  
 Q: Variables in associative array names


 >
 >>
 >> The intention of the following code is to set an element in ten
 >> associative arrays:
 >>
 >>         for {set i 0} {$i <= 10} { incr i} {
 >>
 >>                 set array_$i(value) 987
 >>         }
 >>
 >> The result is the following error:
 >>
 >>         can't read "i(value)": variable isn't array
 >
 >This works, but it seems like it should be possible in one statement:
 >
 >         for {set i 0} {$i <= 10} { incr i} {
 >                 set a array_$i
 >                 set ${a}(value) 987
 >         }
 >
 >

The suggestions are all a bit tortuous!

Try replacing the two set statements above with the following:-

        set array_${i}(value) 987

The {} act as delimiters for the variable substitution in the parser,
so the desired substitution will take place.

You could also do the following:-

        set array($i,value) 987

or

        set array($i value) 987

where "," or a space are used as delimiters, providing they cannot appear
in $i. This would be a better and easier to understand way of doing things,
but this may prevent you using some of the array commands.

Good luck.

Tony



Sun, 29 Nov 1998 03:00:00 GMT  
 
 [ 9 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. More About Array Names In String Variables

7. Array variable name decomposition - kludge leads to bug

8. associative arrays in J?

9. Associative Array Performance

10. associative arrays

11. Associative Arrays

12. associative arrays/memory

 

 
Powered by phpBB® Forum Software