... this could have been done via grep in clever ways ...
You may be thinking of something like:
c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @list = (rand(1), rand(1), rand(1)); dd \@list; ;; my $less = 1; for (@list) { $less = 0, last if $_ >= 0.5; } print 'for: all satisfy the condition' if $less; ;; my $any = grep { $_ >= 0.5 } @list; print 'grep: none fail the condition' if not $any; " [0.860137939453125, 0.796112060546875, 0.875457763671875] c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @list = (rand(1), rand(1), rand(1)); dd \@list; ;; my $less = 1; for (@list) { $less = 0, last if $_ >= 0.5; } print 'for: all satisfy the condition' if $less; ;; my $any = grep { $_ >= 0.5 } @list; print 'grep: none fail the condition' if not $any; " [0.134674072265625, 0.332855224609375, 0.4288330078125] for: all satisfy the condition grep: none fail the condition
Because grep must continue to grind through all remaining elements of the list even after it is determined that the overall condition has failed, I'm not sure I would consider this "clever." Quick and dirty, yes, and useful as such.
The for-loop approach has the advantage that it stops immediately when the overall condition is determined to have failed. The guts of List::MoreUtils any() and all() and their ilk are based on for-loops. An extra variable is a small price to pay.
Give a man a fish: <%-{-{-{-<
In reply to Re: Fall through loop
by AnomalousMonk
in thread Fall through loop
by b4swine
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |