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 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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?

Thanks!

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:
Code:
#!/usr/bin/tcl

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!

BL

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 www.opentalk.org.uk 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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho