Is there something like `last` for `map`?
In Perl, is it possible to arbitrarily end a map
execution, e.g. something equivalent to last
in a loop?开发者_JAVA技巧
It would be a bit like this:
map {
if (test == true) { last; } dosomething
} @myarray
Nope. You can't last
, next
, etc. out of a map
, grep
or sort
blocks because they are not loops.
Having said that, the code snippet in the question is poor practice because map
should never be called in void context. They are supposed to be used to transform one list into another.
Here's a better way to write it if you really must inline it (modified after ysth's comment):
$_ == 10 && last, print for 1..15; # prints '123456789'
No. Use an ordinal foreach
loop and the last
statement.
Since 5.8.1, map is context aware - in void context, no lists are constructed. Anyway, map is generally used to get a list from another list, evaluating expr for each element of the original list.
You could use a do-block with a for statement modifier:
do {
last if test;
dosomething;
} for (@myarray);
Though using a foreach block would probably be clearer, and future maintainers of your code will thank you.
foreach (@myarray) {
last if test;
dosomething;
}
You can use a long jump (eval
/die
pair) to bail out of any nested construct that doesn't directly support it:
eval { map{ die if test; dosomething } @myarray };
But as Zaid said, using a for
/foreach
loop in this case is better because you are not using the return value of map
.
You want a for loop:
foreach ( @myarray ) {
last if test;
...
}
It does the same thing. map
is for transforming one list into other lists.
There are map-like constructs that do exactly what you want to do. Take a look at List::Util and List::MoreUtils (conveniently also packaged together as List::AllUtils):
use List::MoreUtils 'first';
# get first element with a {foo} key
my $match = map { $_->{foo} eq 'some string' } @elements;
If you don't want to extract an element(s) from the list, then use foreach, as per the previous answers.
Try goto LABEL. However I do not know how safe is that.
精彩评论