Note I've used our for the index cache, so that it is a global variable and thus will persist. This solution is not thread safe, but could be made so.our $i_cache; for my $i ((defined $i_cache ? $i_cache+1 : 0) .. $#array) { $i_cache = $i; if(CONDITION_MET){ last; } } undef $i_cache if $i_cache == $#array; # Reset if we went the whole wa +y
It's worth noting that the follow will not work:
When you use a variable with a scope outside a loop as the loop variable, is is automatically localized, so it won't give you a hint as to where the loop ended.our $i; for $i ($i ..10) { last if $i == 5; } print $i;
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
In reply to Re: Breaking from a foreach loop, returning to position
by kennethk
in thread Breaking from a foreach loop, returning to position
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |