in reply to Breaking from a foreach loop, returning to position

Based on what you've described here and elsewhere in the thread, your solution could be as easy as:
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
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.

It's worth noting that the follow will not work:

our $i; for $i ($i ..10) { last if $i == 5; } print $i;
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.

#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
    I'm having some trouble implementing your solution. When I had browsed the internet before posting, I came across similar syntax in the for statement, and I think it has to do with my array that is being iterated. When I would print out $_, or in this case, $i, instead of giving me the actual data, I get the index of the array. How would I print out, and use, the actual data in the array? Would using a C-style $array_scalar = $array[$i] work, or is this syntax not supported in Perl?
      When I would print out $_, or in this case, $i, instead of giving me the actual data, I get the index of the array. How would I print out, and use, the actual data in the array? Would using a C-style $array_scalar = $array[$i] work, or is this syntax not supported in Perl?

      Sigh. How hard can it be to type that into your script and TRY IT?

      BTW: The usual way to filter lists (including arrays) is to use grep if you want a list of all matching elements. E.g. to get a list of all even numbers in the range from 1 to 10:

      >perl -E 'say for grep { $_%2==0 } (1..10)' 2 4 6 8 10 >

      (On Windows, use double quotes instead of single quotes.)

      List::Util can do more:

      What is the FIRST even number in that range?

      >perl -E 'use List::Util qw( first ); say first { $_%2==0 } (1..10)' 2 >

      Are there ANY even numbers in that range?

      >perl -E 'use List::Util qw( any ); say any { $_%2==0 } (1..10)' 1 >

      (any() returns a boolean value, so 1 means "yes", not "there is only one even number")

      Are ALL numbers in that range even?

      >perl -E 'use List::Util qw( all ); say all { $_%2==0 } (1..10)' >

      (Again a boolean result, an empty string or undefined value. Both are false, telling you that the range contains numbers that are not even.)

      What's the SUM of all even numbers in that range?

      >perl -E 'use List::Util qw( sum ); say sum grep { $_%2==0 } (1..10)' 30 >

      And so on ...

      Hint: List::MoreUtils knows even more tricks.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Given an index $i, you can access the associated entry in an array with $array[$i]. See Arrays in perlintro, and consider picking up a copy of Introduction to Perl, or peruse http://learn.perl.org/books/beginning-perl/ for a free resource.


      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.