Re: Simple question: setting a variable using a variable as part of the name



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
.



Relevant Pages

  • Re: Persisting env vars in cmd windows
    ... more about batch files as you go on, ... echo The time is %time::=.% ... But the piece de resistance was the "start" command, ... echo:: ERROR ERROR ERROR ERROR ...
    (microsoft.public.win2000.general)
  • Re: Persisting env vars in cmd windows
    ... But the piece de resistance was the "start" command, ... - Have all code for the one project in one single batch file. ... @echo off ... echo:: ERROR ERROR ERROR ERROR ...
    (microsoft.public.win2000.general)
  • Re: Persisting env vars in cmd windows
    ... even though it applies to any command, e.g. echo, copy, ... Similarly the syntax for the extremely powerful "for" command ... more about batch files as you go on, ... echo:: ERROR ERROR ERROR ERROR ...
    (microsoft.public.win2000.general)
  • Re: Persisting env vars in cmd windows
    ... even though it applies to any command, e.g. echo, copy, ... Similarly the syntax for the extremely powerful "for" command ... more about batch files as you go on, ... echo:: ERROR ERROR ERROR ERROR ...
    (microsoft.public.win2000.general)
  • Re: Persisting env vars in cmd windows
    ... I open several Command windows in succession. ... Drag the name of C000_CreateSymbols.bat into a new command window ... @echo off ... echo:: ERROR ERROR ERROR ERROR ...
    (microsoft.public.win2000.general)