打破父母循环在TCL [英] Breaking parent loop in tcl
问题描述
我在while循环中有一个for循环。
以下是代码:
while {[gets $ thefile line]> = 0} {
for {set i 1} {$ i <$ count_table} {incr i} {
if { regexppattern_ $ i$ line} {
for {set break_lines 1} {$ break_lines< $ nb_lines} {incr break_lines} {
if {[gets $ thefile line_ $ break_lines]< 0} break
}
}
#some其他进程做
}
我想在分析的文件中跳过 $ nb_lines
来做更多的事情。这里break,打破for循环,所以它不起作用。
for循环可以使while循环断开吗?
但破解只是1(或更多)行,我想继续解析文件中断后进一步处理行
谢谢 break
命令(和 continue code>)也不会执行多级循环退出。国际海事组织,最简单的解决方法只是重构的代码,所以你可以
返回
退出外部循环。但是,如果你不能这样做,那么你可以使用类似的东西(对于8.5和更高版本):
proc magictrap {code body} {
if {$ code< = 4} {errorbad magic code}; #如果{[catch {uplevel 1 $ body} msg opt] == $ code} return
return -options $ opt $ msg
}
proc为Tcl
保留的值更低magicthrow代码{返回代码$代码不要紧,这是什么}
,而{[获取$文件行]> = 0} {
magictrap 5 {
{for {set i 1} {$ i <$ count_table} {incr i} {
if {[regexppattern_ $ i$ line]} {
for {set break_lines 1} {$ break_lines< ; $ nb_lines} {incr break_lines} {
if {[gets $ thefile line_ $ break_lines]< 0} {magicthrow 5}
}
}
}
}
#s其他进程做
}
$ c $
请注意自定义的异常代码是Tcl的深魔法部分。如果可以的话,请使用普通的重构。 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 : I want to skip 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 The Be aware that custom exception codes are a "deep magic" part of Tcl. Please use ordinary refactoring instead if you can. 这篇关于打破父母循环在TCL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
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
}
$nb_lines
in the file parsed to do other thing further. Here the break, breaks the for loop, so it doesn't work.break
command (and continue
too) doesn't do multi-level loop exit. IMO, the simplest workaround is to just refactor the code so you can return
to exit the outer loop. However, if you can't do that, then you can use something like this instead (for 8.5 and later):proc magictrap {code body} {
if {$code <= 4} {error "bad magic code"}; # Lower values reserved for Tcl
if {[catch {uplevel 1 $body} msg opt] == $code} return
return -options $opt $msg
}
proc magicthrow code {return -code $code "doesn't matter what this is"}
while {[gets $thefile line] >= 0} {
magictrap 5 {
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} {magicthrow 5}
}
}
}
}
#some other process to do
}
5
isn'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.)