Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to break or continue in MVEL for / foreach loop

Tags:

mvel

I see MVEL supports for loop and foreach templating, but how to "break" or "continue" from the loop?

like image 399
D.S Avatar asked Mar 07 '13 02:03

D.S


1 Answers

No mention of support of 'break' or 'continue' in the documentation: http://mvel.codehaus.org/MVEL+2.0+Control+Flow.

The closest I could find is a user group email in 2009, stating that there's NO support of break or continue: http://markmail.org/message/rgyqvwhiedfpcchj

you could still achieve the same effect as "break" this way (not the cleanest code in the world):

skip_rest = false;
for(item: collection) {
   if (!skip_rest) {
     /* do something */
     if (some condition) {
       /* break by skipping */
       skip_rest = true; 
     }
   }
}

You got the idea, similar thing can be done by flag setting to achieve 'continue'.

like image 100
Weiming Avatar answered Nov 14 '22 18:11

Weiming