Re: Simple question: setting a variable using a variable as part of the name
- From: Jonathan Bromley <spam@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 03 Aug 2011 22:30:13 +0100
On Wed, 3 Aug 2011 14:06:59 -0700 (PDT), jktstance
Take this very simple test script:
set p 0
set test$p "Trial"
Obviously, what I'm trying to do here is to set the variable "test0"
as "Trial". However, I can't seem to find a combination of braces or
quotes on the test$p variable to get it to work.
I know that if I separate the "test" and $p with parenthesis like
set test($p) "Trial"
then it works fine. I'm just curious if it's possible to do this
without adding parenthesis to the variable name.
Like they said before:
(1) puts [set test$p]
(2) better: use arrays, like the way you found to work
BTW I guess you're aware that "echo" isn't a native Tcl
command? If you say "echo $foo" at a regular tclsh
prompt, tclsh notices that "echo" is not a Tcl command
nor an abbreviation of a command, so it then invokes
the OS "echo" command, having first provided the
substituted value of $foo. Probably best to get into
the habit of using puts in preference to echo, to avoid
nasty surprises later.
- Prev by Date: Simple question: setting a variable using a variable as part of the name
- Next by Date: Re: bind and 'all' keyword
- Previous by thread: Simple question: setting a variable using a variable as part of the name