Version 12 of continue

Updated 2005-04-20 17:51:39 by lwv

http://purl.org/tcl/home/man/tcl8.4/TclCmd/continue.htm


LES: Suppose the following code:

 foreach  i  $List1  { 

     foreach  j  $List2  { 

        if  [some condition] {continue}

        eval {blah blah blah}
     }
 }

Continue interrupts the foreach j loop. But what if I wanted it to break the foreach i loop?

Place the [foreach i] command in a proc and use return instead of continue.

Lars H: Or use breakeval as follows

  foreach i $List1 { 
     breakeval-aware foreach j $List2 { 

        if  {[some condition]} then {
           breakeval {continue} ; # Break out of j loop, do a continue for the i loop.
        }

        eval {blah blah blah}
     }
 }

MG April 20th 2005 - In PHP, continue (and break) accept an argument that tells it how many nesting levels to continue/break for, slightly-similar to return's -code option. How hard would it be to add something like that into Tcl? Since neither break nor continue accept any arguments right now, as long as it defaulted to 1 it would have total backwards-compatability, and would, IMHO, be more "natural" than the above.

schlenk Did you try return -code continue -level ...

LES: These suggestions all involve creating yet another proc for my code, which is not really what I was looking for. I was indeed thinking of PHP's continue/break argument, as MG mentioned. I asked the question in the first place because I already have a solution, but it forces me to add more code to an already very long proc that I am trying to do some liposuction on. Some sort of "loop depth argument" would help me make it shorter. Anyway, here is my current solution:

 foreach  i  $List1  { 

     set  _break  0
     foreach  j  $List2  { 

        if  [some condition] {incr _break;  break }

        eval {blah blah blah}
     }

     if { $_break > 0 }  { continue }
 }

Conclusion: if you come from PHP, now you know that there is no continue/break "loop depth argument". But several workarounds are offered above (and below?).

LV Given that there's already the weird return arguments, what is the argument against enhancing continue as described above? Is there a technical issue with the proposal?


[ Tcl syntax help | Arts and crafts of Tcl-Tk programming | Category Command | Category Control Structure ]