Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Ways to control a map() operation

by ioannis (Prior)
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://561482]
help
Chatterbox?
[zentara]: marto arn't those NSA secrets? :-)
[marto]: not if you keep telling people :P
[zentara]: I had to disable hyperthreading on my machine because of that.
[Discipulus]: thanks marto but... at the moment this seems a bit beyond my faculties.. ;=)
[zentara]: a thread goes into a wait state after emitting a wait to all other threads. It locks up a machine completely.
[marto]: if you mean the recent hyperthreading bug, I don't think that's NSA related?
[zentara]: s/thread/cpu/
[marto]: xkcd://538
[Discipulus]: these opcodes mentioned in the wiki pages are the same opcode perl is translated into? or is just the same term but in different fields?

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2017-07-28 12:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (428 votes). Check out past polls.