Reputation: 305
I have a script with this snippet:
while {[gets $fin line] != -1} {
if {[string first "Modem :" $line] != -1} {
set line2 [string range $line 17 end]
puts $fout "One : \t$line2"
}
puts $fout "Two : \t$line2"
}
The One :
works and prints the output (when i don't include the Two :
part in the script) but when I include Two :
, it shows
error : can't read "line2": no such variable
while executing
"puts $fout "Two : \t$line2""
("while" body line 14)
Doesn't it hold the value of line2
after coming out of if
?
Upvotes: 0
Views: 1828
Reputation: 71538
From chat, this is a sample of $fin
.
The problem with the code is that while {[gets $fin line] != -1}
loops through each line of $fin
one at a time, and not a whole bunch together. read
is the command that gets all lines in one variable.
This means that when the first line is read, you don't have $line1
or $line2
in the first iteration of the loop and thus, puts
will fail to retrieve a variable by those names.
My proposed solution is to get each of the required variables first and when everything is gathered for the 'blocks', print them all at once.
set fin [open imp_info r]
set fout [open imfp_table w]
puts $fout "LINK\tModem Status"
puts $fout "----\t------------"
while {[gets $fin line] != -1} {
# If there is "wire-pair (X)" in the line, get that number
regexp -- {wire-pair \(([0-9]+)\)} $line - wire_pair_no
# Last column, get the value and puts everything
if {[regexp -- {Modem status: ([^,]+)} $line - status]} {
puts $fout "$wire_pair_no\t$status"
}
}
Output:
LINK Modem Status
---- ------------
0 UP_DATA_MODE
1 UP_DATA_MODE
Upvotes: 1
Reputation: 137567
If with the first line you read in this loop:
while {[gets $fin line] != -1} {
if {[string first "Modem :" $line] != -1} {
set line2 [string range $line 17 end]
puts $fout "One : \t$line2"
}
puts $fout "Two : \t$line2"
}
you do not have a “Modem :
” somewhere on it, the condition is not satisfied and the variable is not touched when processing the if
. The following puts
fails because the line2
variable hasn't been set to anything yet; there's just no variable there, and the $
syntax doesn't like that at all.
One possible fix is to set line2
to something before the start of the loop:
set line2 "DUMMY VALUE"
Or maybe you should change what variable you're reading there to line
instead of line2
.
puts $fout "Two : \t$line"
Or maybe you should test whether the variable exists before reading it:
if {[info exists line2]} {
puts $fout "Two : \t$line2"
}
All will work, but they do different things and I don't know what you want…
Upvotes: 0