Re: Dynamic variables and lists
- From: Bryan Oakley <oakley@xxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 29 Jun 2005 19:53:23 GMT
ssriperu1@xxxxxxxxx wrote:
Here's my problem:
% set a power power % set ${a} [list a b c d] a b c d % set second [lindex ${a} 1]
Since a contains "power", the above is identical to:
set second [lindex "power" 1]
% puts $second # prints nothing
.... because "power", when treated like a list, has only 1 element.
% set second [lindex eval ${$a} 1]
The above is wrong on many levels. Read the man page on lindex and you'll see that the first argument is the list to be operated on. In the above you're looking into a list that contains the word "eval". Not what you indented, I'm sure.
can't read "$a": no such variable
That is because there is no variable named "$a" (dollar sign, letter a)
% puts $a power
Correct. The variable "a" (*not* the variable $a) contains the word "power"
% set second [lindex eval $a 1] bad index "power": must be integer or end?-integer?
Again, look up the definition of the lindex command.
How do I get access to the $power list without specifying "power" ?
how about set second [lindex [set $a] 1]
Remember that 'set' returns the value of the variable. Also remember that tcl does substitution before calling a command. Thus, [set $a] in this case is identical to [set power], which gives the value stored in the variable named "power".
Since you are just now learning tcl I strongly recommend you do not try to use dynamic variables. Try using an array, they are much easier to use:
set a "power"
set data($a) [list a b c d]
set second [lindex $data($a) 1]
.- References:
- Dynamic variables and lists
- From: ssriperu1
- Dynamic variables and lists
- Prev by Date: Re: Dynamic variables and lists
- Next by Date: oratcl and child procs
- Previous by thread: Re: Dynamic variables and lists
- Next by thread: oratcl and child procs
- Index(es):
Relevant Pages
|