Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: map vs for\foreach.

by MidLifeXis (Monsignor)
on Mar 11, 2015 at 14:44 UTC ( [id://1119648]=note: print w/replies, xml ) Need Help??


in reply to Re^2: map vs for\foreach.
in thread map vs for\foreach.

That is essentially what a map is - although your syntax is wrong. Unless you do something like: map { sub {...}->(...) } @foo to force the execution of the sub, you will simply get a set of sub {...} anonymous subroutines the same size as @foo.

The contents of the BLOCK in the map call are what I think you are thinking of as the sub. OTOH, something like:

@subs = map { my $something = $_; sub { blah( $something, ... ); } } @data;

would give you a set of subroutines that each operate on a specific item in @data. In this case, you are still transforming @data into a set of subs working on a closures.

--MidLifeXis

Replies are listed 'Best First'.
Re^4: map vs for\foreach.
by bibliophile (Prior) on Mar 13, 2015 at 13:35 UTC
    Ok, that just blew my mind. (Gotta love those Perl "aha!" moments!)

    Thanks, MidLifeXis :-)

    -Bib

Log In?
Username:
Password:

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

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

    No recent polls found