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!

arrays

Status
Not open for further replies.

Thodd

Technical User
Oct 21, 2006
27
BE
I have 10 files which contain many lines with timestamps. Those timestamps are read and put in arrays beforehand in while-loops like:

set chan [open "file1.tr"]
set lineNumber 0
while {[gets $chan line] >= 0} {
scan $line "%f" time1($lineNumber)
$ns at $time1($lineNumber) "do something"
incr lineNumber
}
set chan [open "file2.tr"]
...

Later on, I try to call the values of those arrays, but in a generic manner:

for {set x 1} {$x <= 10} {incr x} {
set time "time"
append time $x

puts "file $x :"
puts " line 50 = $time(50)
}

but this doesn't seem to work because $time isn't an array here...
How can I do this?
thanks
 
time should not be a variable, but an array, like:
time1()
time2()
...

the values of those arrays are the timestamps (which are read before from the files).

I like to print some values of those arrays. So I tried the following, before the for-loop:
array set time

but that doesn't work either.
 
ok I changed my code a little bit. For each file I'm reading it needs to store the time stamps in an array. I do it like this:

for {set i 1} {$i <= 10} {incr i} {
set file "file"
append file $i ".tr"
set chan [open $file]
set lineNumber 0
set timearray "time"
append timearray $i
array set $timearray ""


while {[gets $chan line] >= 0} {
scan $line "%f" time
array set $timearray {$lineNumber $time}
$ns at [eval set "time"] "do_something"
incr lineNumber
}
}

Later on, I try to print the values of those arrays, but in a generic manner:

for {set x 1} {$x <= 10} {incr x} {
set timeprint "time"
append timeprint $x

puts "file $x :"
puts "[array get $timeprint]"
}

but this doesn't work :s
what am I doing wrong?
 
array set does not grow the array; it resets the entire array, in this case the array whose name is the current value of timearray. So each iteration of your while loop resets the entire array to contain a single element whose index is the current value of lineNumber and whose value is the current value of time. It's likely that you didn't mean to do that.

_________________
Bob Rashkin
 
thx, so how can I grow those arrays?
 
The simplest would be to grow a list. In each iteration of i, initialize a list: set tmlst "". Then, in each iteration of the lineNumber, add to the list: lappend tmlst "$lineNumber $time". Then, after the while loop but still in the same iteration of the for loop, set your array: array set $timearray $tmlst

_________________
Bob Rashkin
 
ok
but now there's an error:
list must have an even number of elements while executing
"array set $timearray $tmlst"
 
Does anybody know how this can be solved?
I don't see what's wrong here. :s

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

puts "Reads file $i ..."
while {[gets $chan line] >= 0} {
scan $line "%f" time
lappend tmlst "$lineNumber $time"
$ns at [eval "set time"] "do_something"
incr lineNumber
}

close $chan
set timearray "time"
append timearray $i
array set $timearray $tmlst
}

-> list must have an even number of elements while executing
"array set $timearray $tmlst"

thanks
 
My mistake. You don't need the quotes (""). Grow your list without the quotes:
lappend tmlst $lineNumber $time

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top