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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

constructing array-variables

Status
Not open for further replies.

Thodd

Technical User
Oct 21, 2006
27
BE
Hi, I have some probs with constructing array-variables in a for-loop. My code looks like this:

for {set i 1} {$i <= 3} {incr i} {
set chan [open "file.tr"]
set lineNumber 0

while {[gets $chan line] >= 0} {
scan $line "%f" time1b($lineNumber)
$ns at $time1($lineNumber) "procedure"
incr lineNumber
}

close $chan
}

This while-loop works only for i=1 (see time1)
Now I'm trying to make it more generic (for other values i). How can I do this? I've got something like:

set time "time"
append time $i

while {[gets $chan line] >= 0} {
scan $line "%f%d%n" time($lineNumber)
$ns at $time($lineNumber) "procedure"
incr lineNumber
}

but this isn't working :s
Thx!
 
I made a mistake with the html-tags, my code looks like:

for {set i 1} {$i <= 3} {incr i} {
set chan [open "file.tr"]
set lineNumber 0

while {[gets $chan line] >= 0} {
scan $line "%f" time1($lineNumber)
$ns at $time1($lineNumber) "procedure"
incr lineNumber
}

close $chan
}
 
I'm not clear what your code does; I can't see what $ns is, for instance. But generally, the way to build a variable name is to use eval as in
set cname time
append cname $i
append cname ($lineNumber)
eval "$ns at $cname ..."


_________________
Bob Rashkin
 
Thx for your advice! $ns isn't so important here, it's just a variable for my NS-2. It calls a procedure at some time. But I have another problem now.
file.tr consists of lines with those time value. So I'm trying to print some of those values on screen afterwards. My code looks like:

for {set i 1} {$i <= 3} {incr i} {
set chan [open "file.tr"]
set lineNumber 0

while {[gets $chan line] >= 0} {
set time "time"
append time $i ($lineNumber)
scan $line "%f" time
$ns at [eval "set time"] "procedure"
incr lineNumber
}

close $chan
}
puts "$time2(50) = time value on line 50"

But the compiler doesn't know this variable. It seems it's been overwritten... How can I get this fixed?
 
You never set timeX(i) to anything. You only retrieve it's current value with [eval "set time"]. I think you're making this harder than it needs to be.

You only have one file, "file.tr". You shouldn't open it and read it every iteration since you don't do anything to the file that depends on anything inside the loop. Open it, read it all into a list and then close it, before you start looping:
Code:
set chan [open file.tr r]
set chanlist [split [read $chan] \n]
close $chan
now chanlist is a Tcl list of all the lines in "file.tr".

Now, as sexy as it may seem to have everything driven by your loop variable, you only have 3 values, 1,2 and 3. Why not construct your "time" array as 2-dimensional, with time(1,n) being time1(n), etc.?


_________________
Bob Rashkin
 
I'm sorry, my mistake, I do have several files:

for {set i 1} {$i <= 3} {incr i} {
set filename "file"
append filename $i ".tr"
....
set chan [open $filename]
set lineNumber 0

while {[gets $chan line] >= 0} {
...
}

close $chan
}
puts "$time2(50) = time value on line 50"

So I need to open every file with those time values after each other and call a procedure. after the for-loop I'd like to print some time values.
 
OK, but the 2-D array is still simpler, I think. As I see it, you have three files. For each line in each of the files, you process the line to get a time, which you add to an array. Then you want to print some of it. If that's right, I think you might try something like the following:
Code:
foreach fname {file1 file2 file3} {
   set fid [open $fname.tr r]
   set lines [split [read $fid] \n]
   close $fid
   set linenum 0
   foreach line $lines {
     #do whatever
     scan $line %f [red]timearray($fname,$linenum)[/red]
     #do whatever
     incr linenum
   }
}
puts $timearray(file2,50); #or whatever

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top