For loop 在tcl中中断父循环

For loop 在tcl中中断父循环,for-loop,while-loop,tcl,For Loop,While Loop,Tcl,我在while循环中有一个for循环。在for循环中,我有一个条件来中断该循环 代码如下: 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}

我在while循环中有一个for循环。在for循环中,我有一个条件来中断该循环

代码如下:

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
}
而{[get$offile line]>=0}{

对于{set i 1}{$i可能很明显,但是您可以使用附加变量(
go_on
)来中断while:

while {[gets $thefile line] >= 0} {
  set go_on 1
  for {set i 1} {$i<$count_table && $go_on} {incr i} {
    if { [regexp "pattern_$i" $line] } {
      for {set break_lines 1} {$break_lines<$nb_lines && $go_on} {incr break_lines} {
        if {[gets $thefile line_$break_lines] < 0} { set go_on 0 }
      }
     }
   }
   #some other process to do
}
而{[get$offile line]>=0}{
将go_设置为1

对于{set i 1}{$i来说,
break
命令(以及
continue
命令)不会执行多级循环退出。在我看来,最简单的解决方法是重构代码,这样你就可以返回
退出外部循环。但是,如果你做不到,那么你可以使用类似的方法(对于8.5及更高版本):

proc magictrap{code body}{
如果{$code=0}{
魔法地图5{

对于{set i 1}{$iHi,好吧,这可以暂时停止。但是这是一个太难的中断,我无法继续解析文件,我也无法在#点上进行其他处理。你有继续解析的想法吗?(我编辑了一点我的问题)我不太清楚您想要做什么,但是这可能会起作用。我们快结束了,但它不好。这是我的错,我不够清楚。我想解析一个文件,而在解析过程中,我正在搜索非规则的模式(这就是为什么我要执行for循环)。当我找到模式时,我想跳过行以进一步处理行。我想中断while,仅用于1(或更多)行,但不停止解析。如果您想知道,0表示正常成功,1表示错误(并导致在展开过程中生成堆栈跟踪),2表示从当前程序返回,3表示中断,4表示继续。
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
}