For Programmers: Free Programming Magazines  


Home > Archive > Tcl > August 2007 > Dynamic variable naming









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Dynamic variable naming
gcgaim@gmail.com

2007-08-24, 4:27 am

Hi,
I'm having a problem doing something that I know you can do in TCL.
Let's say I have a var

set ::ns::subns::var test

I then have a proc which takes in a namespace and a sub namepsace as
its params:

proc testproc { namespace subnamespace } {
....
}

I want to print out the value 'test' if someone calls it as:

testproc { "ns" "subns" }

So I'm looking for a way to eval a variable when you don't know the
name of the variable up front. ANy help is appreciated!

Eric Hassold

2007-08-24, 4:27 am

Hi,

gcgaim@gmail.com wrote :
> Hi,
> I'm having a problem doing something that I know you can do in TCL.
> Let's say I have a var
>
> set ::ns::subns::var test
>
> I then have a proc which takes in a namespace and a sub namepsace as
> its params:
>
> proc testproc { namespace subnamespace } {
> ...
> }


[set varName] returns the value of variable varName. So all you have to
do is built varName from procedure arguments:

proc testproc {namespace subnamespace} {
return [set "::${nanespace}::${subnamespace}::var"]
}

Note that "return" and quotes are useless, added there only for clarity.

>
> I want to print out the value 'test' if someone calls it as:
>
> testproc { "ns" "subns" }


From prototype you gave above, someone will actually call it as:
% testproc "ns" "subns"

>
> So I'm looking for a way to eval a variable when you don't know the
> name of the variable up front. ANy help is appreciated!
>


Eric

-----
Eric Hassold
Evolane - http://www.evolane.com/
gcgaim@gmail.com

2007-08-24, 4:27 am

On Aug 23, 1:17 pm, Eric Hassold <hass...@evolane.com> wrote:
> Hi,
>
> gcg...@gmail.com wrote :
>
>
>
>
>
> [set varName] returns the value of variable varName. So all you have to
> do is built varName from procedure arguments:
>
> proc testproc {namespace subnamespace} {
> return [set "::${nanespace}::${subnamespace}::var"]
>
> }
>
> Note that "return" and quotes are useless, added there only for clarity.
>
>
>
>
>
> From prototype you gave above, someone will actually call it as:
> % testproc "ns" "subns"
>
>
>
>
> Eric
>
> -----
> Eric Hassold
> Evolane -http://www.evolane.com/


Thanks :)

Larry W. Virden

2007-08-24, 4:27 am

On Aug 23, 1:17 pm, Eric Hassold <hass...@evolane.com> wrote:
> gcg...@gmail.com wrote :


>
>



[color=darkred]
> [set varName] returns the value of variable varName. So all you have to
> do is built varName from procedure arguments:
>
> proc testproc {namespace subnamespace} {
> return [set "::${nanespace}::${subnamespace}::var"]


note that needs to be ${namespace}, not ${nanespace}

>
> }


I started to respond with something similar, until I hit that last
nite. Maybe I'm reading way too much into that sentence. But it reads
as if the Original Poster didn't know that the value "test" was going
to be in variable "var" and s/he needed to introspectively determine
what variables were in in the subnamespace and then figure out which
one to print. There wasn't enough requirements to tell us how one
would decide which variable would be displayed - perhaps all of them?


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com