Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Syntax Problem - Take a look

Status
Not open for further replies.

BrianLeighton

Programmer
Nov 14, 2002
2
US
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 &quot;STATS$x&quot; ; #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!
 
Try this (it looks odd but works!!)
Code:
puts [set STATS[set x](-VPI)]
Hope this helps.
 
Does this work for you?
proc printIncArr {array cnt} {
for {set x 0} {$x < $cnt} {incr x} {
set name [join $array$x]
upvar #0 $name loc
puts &quot;[set name] is $loc(-VPI)&quot;
}
return
}
 
Thanks ovey and marsd! Both solutions worked, but ovey's was exactly what I needed. Being new to TCL I forget that the &quot;set&quot; command also can be used to return a value rather than just for defining a variable. Thanks again!

Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top