Variable variable names? 
Author Message
 Variable variable names?

Now,
set datatype mythingie
set ${datatype}_declared_at [unixtime]

works as expected, but, how do i access the data stored in the var, since
if i use

[ctime ${datatype}_declared_at] it uses the name and not the data (and
that is pretty naturally), but i dont suppose that
${${datatype}_declared_at} works?
So, how do i read my dear old unixtime from such a variable?

Thanks.

/jan



Wed, 31 Dec 2003 18:42:41 GMT  
 Variable variable names?
Try ${{datatype}_declared_at}
Quote:

>Now,
>set datatype mythingie
>set ${datatype}_declared_at [unixtime]

>works as expected, but, how do i access the data stored in the var, since
>if i use

>[ctime ${datatype}_declared_at] it uses the name and not the data (and
>that is pretty naturally), but i dont suppose that
>${${datatype}_declared_at} works?
>So, how do i read my dear old unixtime from such a variable?

>Thanks.

>/jan



Wed, 31 Dec 2003 20:01:35 GMT  
 Variable variable names?


Quote:
> Try ${{datatype}_declared_at}

Eh? I get "can't read "{datatype": no such variable"

What about [set ${datatype}_declared_at]

--
Jonathan E. Guyer

<http://www.his.com/jguyer/>



Wed, 31 Dec 2003 21:56:59 GMT  
 Variable variable names?


Quote:

> Now,
> set datatype mythingie
> set ${datatype}_declared_at [unixtime]

An easier way to accomplish this is by using an array:

    set declared_at($datatype) [unixtime]

To answer the specific question it's important to remember (or learn...)
that the set command has two forms -- one to set a value and one to retrieve
a value. So, the idiomatic way to do what you want is this:

    set ${datatype}_declared_at

The ${datatype} will be evaluated and converted to "mythingie" before being
passed to the set command. The set command will thus see
"mythingie_declared_at", consider that the name of a variable, and fetch the
value of that variable.



Wed, 31 Dec 2003 23:05:12 GMT  
 Variable variable names?

Quote:
> Now,
> set datatype mythingie
> set ${datatype}_declared_at [unixtime]
> works as expected, but, how do i access the data stored in the var, since
> if i use
> [ctime ${datatype}_declared_at] it uses the name and not the data (and
> that is pretty naturally), but i dont suppose that
> ${${datatype}_declared_at} works?
> So, how do i read my dear old unixtime from such a variable?

I don't know it is a best way, but you can use:
[set ${datatype}_declared_at]

This should return value which you want get... (set without second argumet,
just returns value in variable)
Anyway, I think is better to use array for this kind of stuff.

Best regards,
TaKeDa



Thu, 01 Jan 2004 02:34:48 GMT  
 Variable variable names?
Try using this:

[ctime [set ${datatype}_declared_at[unixtime]]]

This provided there is a value within this var-within-var

Phil


Quote:

> Now,
> set datatype mythingie
> set ${datatype}_declared_at [unixtime]

> works as expected, but, how do i access the data stored in the var, since
> if i use

> [ctime ${datatype}_declared_at] it uses the name and not the data (and
> that is pretty naturally), but i dont suppose that
> ${${datatype}_declared_at} works?
> So, how do i read my dear old unixtime from such a variable?

> Thanks.

> /jan



Thu, 01 Jan 2004 02:42:34 GMT  
 Variable variable names?

Quote:
> I don't know it is a best way, but you can use:
> [set ${datatype}_declared_at]

> This should return value which you want get... (set without second argumet,
> just returns value in variable)
> Anyway, I think is better to use array for this kind of stuff.

Thankyou, more people pointed out the array aproach, so i will use that..
Question do, is ther eanyway to do a test if a certain element in an array
exists? like an info exists but for elements in arraay?

Thankyou

Jan



Thu, 01 Jan 2004 08:39:43 GMT  
 Variable variable names?

Quote:
> Eh? I get "can't read "{datatype": no such variable"

> What about [set ${datatype}_declared_at]

Wroks, but someone else pointed out i should go with arrays, so ill do
that. But thankyou nonetheless, tyou did after all answer my question as i
had put it :)


Thu, 01 Jan 2004 08:38:31 GMT  
 Variable variable names?

Quote:
> Try using this:

> [ctime [set ${datatype}_declared_at[unixtime]]]

> This provided there is a value within this var-within-var

> Phil

A thankyou to yuo and all the others who helped me on thsi one :)


Thu, 01 Jan 2004 08:41:05 GMT  
 Variable variable names?

Quote:
> An easier way to accomplish this is by using an array:

>     set declared_at($datatype) [unixtime]

Ah. much easier :) But that gives a followup question. I cant seem to be
able to use "info exists" on an element in an array, how do i check if an
element exists within an array?

Thanks



Thu, 01 Jan 2004 08:36:43 GMT  
 Variable variable names?

Quote:
>Question do, is ther eanyway to do a test if a certain element in an array
>exists? like an info exists but for elements in arraay?

Yes. 'info exists' works also for array entries.

% set foo(bar) baz
baz
% info exists foo(bar)
1
% info exists foo(boo)
0

rolf



Thu, 01 Jan 2004 11:03:42 GMT  
 Variable variable names?

Quote:

> > An easier way to accomplish this is by using an array:

> >     set declared_at($datatype) [unixtime]

> Ah. much easier :) But that gives a followup question. I cant seem to be
> able to use "info exists" on an element in an array, how do i check if an
> element exists within an array?

info exists declared_at($datatype) ;# works
info exists $declared_at($datatype) ;# doesn't work


Thu, 01 Jan 2004 12:53:32 GMT  
 Variable variable names?

Quote:
> Yes. 'info exists' works also for array entries.

> % set foo(bar) baz
> baz
> % info exists foo(bar)
> 1
> % info exists foo(boo)
> 0

Yes, i understand now. Thanks!


Thu, 01 Jan 2004 17:25:37 GMT  
 Variable variable names?

Quote:
> info exists declared_at($datatype) ;# works
> info exists $declared_at($datatype) ;# doesn't work

Aaaaah, i see the light! Thankyou.


Thu, 01 Jan 2004 17:23:55 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. values of variable variable-names

2. Using a variable name for a variable name??

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. Class variables / global variables / Init variables

9. field name and key name as a variable

10. field name and key name as a variable

11. J and variable variables

12. awk variables --> shell variables

 

 
Powered by phpBB® Forum Software