Using a variable name for a variable name?? 
Author Message
 Using a variable name for a variable name??

I am trying to use a variable name to hold the name of an array variable.
I have been able to set this by using the [array set name value_list]
command.  I am having problems getting values back out using the $ syntax.
(I have avoided using the [array get name index] command since index is
a pattern and I only want to get one value).

For example:

% global aa
% set aa(item_1) 10
10
%
% set array_name aa
aa
% set index_name item_1
item_1
% echo $array_name $index_name
aa item_1
% echo $array_name($index_name)
can't read "array_name(item_1)": variable isn't array
% echo $$array_name($index_name)
can't read "array_name(item_1)": variable isn't array
% echo ${$array_name($index_name)}
can't read "$array_name($index_name)": no such variable
% echo ${$array_name}($index_name)
can't read "$array_name": no such variable
% echo $($array_name)($index_name)
$(aa)(item_1)
%

I'm sure there must be a way to do this...

Any ideas?

Thanks,

Ed Hepler



Mon, 01 Feb 1999 03:00:00 GMT  
 Using a variable name for a variable name??

Quote:

> I am trying to use a variable name to hold the name of an array variable.
> I have been able to set this by using the [array set name value_list]
> command.  I am having problems getting values back out using the $ syntax.
> (I have avoided using the [array get name index] command since index is
> a pattern and I only want to get one value).

> For example:

> % global aa
> % set aa(item_1) 10
> 10
> %
> % set array_name aa
> aa
> % set index_name item_1
> item_1
> % echo $array_name $index_name
> aa item_1
> % echo $array_name($index_name)
> can't read "array_name(item_1)": variable isn't array

> I'm sure there must be a way to do this...

> Any ideas?

> Ed Hepler

Hi Ed.  There is a quick and dirty way of doing this...simply do the following:

% echo $array_name([set $index_name])

That way, it's returning the value in item_1, not the value of index_name.

*grin*  

Have a nice day.

Bryce



Mon, 01 Feb 1999 03:00:00 GMT  
 Using a variable name for a variable name??


: I am trying to use a variable name to hold the name of an array variable.
: I have been able to set this by using the [array set name value_list]
: command.  I am having problems getting values back out using the $ syntax.
: (I have avoided using the [array get name index] command since index is
: a pattern and I only want to get one value).

: For example:

: % global aa
: % set aa(item_1) 10
: 10
: %
: % set array_name aa
: aa
: % set index_name item_1
: item_1
: % echo $array_name $index_name
: aa item_1
: % echo $array_name($index_name)
: can't read "array_name(item_1)": variable isn't array
: % echo $$array_name($index_name)
: can't read "array_name(item_1)": variable isn't array
: % echo ${$array_name($index_name)}
: can't read "$array_name($index_name)": no such variable
: % echo ${$array_name}($index_name)
: can't read "$array_name": no such variable
: % echo $($array_name)($index_name)
: $(aa)(item_1)
: %

You need an extra level of evaluation, e.g. using an eval or a set:
% set aa(item_1) 10
10
% set array_name aa
aa
% set index_name item_1
item_1
% set ${array_name}($index_name)
10
% eval puts $${array_name}($index_name)
10
% puts [set ${array_name}($index_name)]
10

The braces are needed around array_name so that the () part isn't considered
an array index for the array_name variable.

Peter
--

Department of Electrical and Computer Engineering
Carnegie Mellon University                             Phone: (412) 268-2687
Pittsburgh  PA  15213  USA                             Fax: (412) 268-2860



Mon, 01 Feb 1999 03:00:00 GMT  
 Using a variable name for a variable name??


Quote:
>I am trying to use a variable name to hold the name of an array variable.
>I have been able to set this by using the [array set name value_list]
>command.  I am having problems getting values back out using the $ syntax.
>(I have avoided using the [array get name index] command since index is
>a pattern and I only want to get one value).

>For example:

>% global aa
>% set aa(item_1) 10
>10
>%
>% set array_name aa
>aa
>% set index_name item_1
>item_1
>% echo $array_name $index_name
>aa item_1
>% echo $array_name($index_name)
> ...

>I'm sure there must be a way to do this...

>Any ideas?

You need a double substitution round :
    i.e  substitute array_name(index_name) to
            aa(item_1)
    then substitute aa(item_1) to 10

    so first round
        ${array_name}($index_name) becomes aa(item_1)

    then substitute that
        $aa(item_1)

# eval
TCL:23 ~/tmp/i> eval echo $${array_name}($index_name)
10

# Use Tcl7.5 subst cmd to get another round of parsingg
TCL:24 ~/tmp/i> subst $${array_name}($index_name)
10

#or cleanest of all use "set var" which returns vars value
TCL:25 ~/tmp/i> set ${array_name}($index_name)
10

--
- hops

Everything disclaimed (including disclaimer)

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



Mon, 01 Feb 1999 03:00:00 GMT  
 Using a variable name for a variable name??



Quote:
>I am trying to use a variable name to hold the name of an array variable.
>% set array_name aa
>% echo $array_name($index_name)

echo [set $array_name\($index_name)] would work.

--
     Jeffrey Hobbs                       office: 541.683.7891

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



Mon, 01 Feb 1999 03:00:00 GMT  
 Using a variable name for a variable name??


+I am trying to use a variable name to hold the name of an array variable.
+% set aa(item_1) 10
+% set array_name aa
+% set index_name item_1
+% echo $array_name($index_name)

Try:
% eval echo $array_name($index_name)

                        Bruce



Fri, 05 Feb 1999 03:00:00 GMT  
 Using a variable name for a variable name??


Quote:

>I am trying to use a variable name to hold the name of an array variable.
>I have been able to set this by using the [array set name value_list]
>command.  I am having problems getting values back out using the $ syntax.
>(I have avoided using the [array get name index] command since index is
>a pattern and I only want to get one value).

In all the replies so far, I have yet to see anyone mention the use of upvar.
Try it like this:

% set aa(item_1) 10
10
% set array_name aa
aa
% set index_name item_1
item_1
% puts "$array_name $index_name"
aa item_1
% puts $array_name($index_name)
can't read "array_name(item_1)": variable isn't array

% upvar 0 $array_name myarray

% puts $myarray($index_name)
10
%

I don't know why it works - but it does :-)



Sat, 06 Feb 1999 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. field name and key name as a variable

2. field name and key name as a variable

3. variable contents to variable name

4. array name in variable variable

5. getting variable name from the variable itself?

6. Variables containing variable names

7. name of a variable stored in another variable

8. Variable variable names?

9. values of variable variable-names

10. changing file directory at runtime without using a variable in prop:name

11. Error in Report Formatter (CFD 3.1) using variables as Printer Names

12. CW2.003 Report Writer - Using variable for file path/name

 

 
Powered by phpBB® Forum Software