Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
TCL Array Syntax problems

Posted by BrianLeighton (BrianLeighton), 14 November 2002
I'm new to TCL and have been trying like crazy to output the value of a certain index in a defined array. The trick is that the array name changes. I have several arrays, named STATS0, STATS1, STATS2,...STATx..and so on. I thought I could just set up a loop to increment the name (i.e STATS$x) and read the index. Here's what I've got so far...

proc writestats {OutputFile CellRate ShowIdle ChannelCount} {
  puts $OutputFile $CellRate
  for {set x 0} {$x<$ChannelCount} {incr x} {
  global STATS$x ; #works fine
  parray "STATS$x" ; #works but prints the whole array
  puts $STATS0(-VPI) ;#works but don't increment obviously
#   puts STATS$x(-VPI) error - don't work
#   puts $STATS$x(-VPI) error - don't work
#   puts [STATS$x](-VPI) error - don't work
#   puts $[STATS$x](-VPI) error - don't work
#   puts (STATS$x)(-VPI) error - don't work
#   puts /$STATS$x(-VPI) error - don't work

Any ideas on how I need to format my syntax to make it work?


Posted by admin (Graham Ellis), 15 November 2002
Taking a step back here and looking at the wider picture.  Do you really need to have separate STAT arrays, or could you make that extra letter onto the end of the variable name into a subscript to the array, and keep the whole thing in an array?

For example:

set route(x,start) Paddington
set route(x,end) "Bristol Temple Meads"

set route(y,start) Waterloo
set route(y,end) Salisbury

foreach num {x y} {
       puts "Travel from $route($num,start) to $route($num,end)"

I'm not saying that you can't find a syntax to solve your original problem, but I would advise that you've chosen a way to do it with a very awkward syntax when having a second subcript provides an easy solution.  Please do post a follow up if there's some reason in the wider application why you can't adopt the type of solution I'm suggesting, and I'll have a closer look at the minutiae of the original syntax and see if I can spot a solution

Posted by BrianLeighton (BrianLeighton), 15 November 2002
The reason I have these seperate STATSx arrays is that the program will have a variable number of arrays built for each run (# of arrays is $ChannelCount). The arrays are generated by getting a statistics string from some test equipment. I read the strings into arrays, and since it's the same stats read into each array all the arrays have identical index values (which is very helpful down the road). I just got this as a suggestion from another forum:

puts [set STATS[set x](-VPI)]

I'll give this a try...Thanks for the reply!

Posted by BrianLeighton (BrianLeighton), 15 November 2002
The "puts [set STATS[set x](-VPI)]" command took care of my problems! Thanks for the reply though!


Posted by admin (Graham Ellis), 16 November 2002
Thanks for letting us know that you've found a solution to fit your particular needs - that's great.  I have to say that I remain somewhat concerned at your use of what is known as a "soft reference" - a variable name partly in another variable name - even though I know that we often do it for Tk widget names. I read what you're doing and think "personally, I would still prefer to use the structure I proposed" ... but then I'm not writing your application, am I

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

1 unpublished comment pending on this page

edit your own (not yet published) comments

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho