Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Ways to control a map() operation

by ioannis (Monsignor)
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";

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://561482]
usemodperl is venting, duh?
[usemodperl]: that was a typos, dumbass
[marto]: usemodperl I guess that depends on what you mean by a safe space, since many people seem to have the impression a safe space allows them to do/say whatever they feel like, without question or critque
[marto]: 'typos'->'typo'
[usemodperl]: it's like you guys are retarded or something, no sense of humor? autism?
[usemodperl]: take things too literally, nothing is funny, everyhting must be perfect, or else, SCOLD SCOLD SCOLD, haha
[Veltro]: usemodperl I think you are offensive right now.
[marto]: people are very defnsive about their bad ideas behaviour' :P
[usemodperl]: sorry veltro, venting...
[usemodperl]: (is that wrong marto?)

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2018-06-24 15:51 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.