Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Ways to control a map() operation

by ioannis (Vicar)
on Jul 15, 2006 at 20:27 UTC ( #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";


Comment on Re: Ways to control a map() operation
Download Code
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
Node Status?
node history
Node Type: note [id://561482]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (17)
As of 2015-07-30 19:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls