Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Ways to control a map() operation

by ioannis (Abbot)
on Jul 15, 2006 at 20:27 UTC ( [id://561482]=note: print w/replies, xml ) Need Help??


in reply to Ways to control a map() operation

You can exit map() with a return, like this: sub{ map { print ; return if 5 >= $_ ; $_ } @arr; }->();

but in doing so, now you can no longer store the map-derived values to a named array, you will have to push them:

# This is no good, values are not stored in @early: sub{ @early = map { print ; return if 5 >= $_ ; $_ } 3..9; }->();

# This is how to store values into @early : sub{ map { print ; return if $_ == 5 ; push @early, $_ } 3..9; }->(); Here is the full snipplet:

my @arr = 3..9 ; our @early; sub{ map { print ; return if $_ == 5 ; push @early, $_ } @arr; }->(); print "@early";

Replies are listed 'Best First'.
Re^2: Ways to control a map() operation
by Aristotle (Chancellor) on Jul 16, 2006 at 12:07 UTC

    What’s the point? Just use a foreach already.

    my @arr = 3..9 ; our @early; sub{ for( @arr ) { print; return if $_ == 5; push @early, $_; } }->(); print "@early";

    Of course, that’s just an obfuscated way of writing the following:

    my @arr = 3..9 ; our @early; for( @arr ) { print; last if $_ == 5; push @early, $_; } print "@early";

    So no, there’s no sensible way to abort a map early.

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://561482]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-18 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found