I have a for loop in a while loop. I have a condition to break the while in the for loop.
Here is the code :
while {[gets $thefile line] >= 0} {
for {set i 1} {$i<$count_table} {incr i} {
if { [regexp "pattern_$i" $line] } {
for {set break_lines 1} {$break_lines<$nb_lines} {incr break_lines} {
if {[gets $thefile line_$break_lines] < 0} break
}
}
#some other process to do
}
I want to skip $nb_lines in the file parsed to do other thing further. Here the break, breaks the for loop, so it doesn’t work.
Can the for loop make the while loop broken ?
But the break is just for 1 (or more) lines, i want to continue to parse the file after the break to process line further
Thanks
The
breakcommand (andcontinuetoo) doesn’t do multi-level loop exit. IMO, the simplest workaround is to just refactor the code so you canreturnto exit the outer loop. However, if you can’t do that, then you can use something like this instead (for 8.5 and later):The
5isn’t very special (it’s just a custom result code; Tcl reserves 0–4, but leaves the others alone) but you need to pick a value for yourself so that it doesn’t overlap with any other uses in your program. (It is mostly possible to redo the code so it works with 8.4 and before too, but it’s quite a bit more complex to rethrow an exception there.)Be aware that custom exception codes are a “deep magic” part of Tcl. Please use ordinary refactoring instead if you can.