Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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]
[virtualsue]: holli about half
[choroba]: "Tatsächlich, das ist die Knochenstruktur eines Bettlers!" ??
shmem bows. Thank you
[marto]: slowclap.gif
[marto]: animated gif in question...
[1nickt]: Interesting article on the Coming Software Apocaplyse that I found while following one of eyepopslikeamosqui to's posts. Posits that no one can guarantee the quality of, or even understand, the 100 million lines of code in a mo
[holli]: you know, monk days are fun and all. but too many of them are bad for your health
[1nickt]: ...modern car. Suggests using TLA+ or other software modeling language to autogenerate code.
1nickt is not sure more and more layers separating the programmer from the program is the right way to go...
[holli]: happy one, shmem ;)

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2017-11-21 12:56 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (301 votes). Check out past polls.