in reply to Breaking from a foreach loop, returning to position
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Breaking from a foreach loop, returning to position
by Anonymous Monk on Aug 26, 2015 at 16:37 UTC | |
by afoken (Chancellor) on Aug 26, 2015 at 19:11 UTC | |
by kennethk (Abbot) on Aug 27, 2015 at 23:18 UTC |