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
<justinthrall@xxxxxxxxx> wrote:
Hi all,
Take this very simple test script:
set p 0
set test$p "Trial"
echo $test$p
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
this:
set test($p) "Trial"
echo $test($p)
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.
--
Jonathan Bromley
.
- References:
- 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
- Index(es):
Relevant Pages
|