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

Re: map and grep

by smahesh (Pilgrim)
on Feb 20, 2007 at 08:17 UTC ( #601051=note: print w/ replies, xml ) Need Help??


in reply to map and grep

In simple terms, use map for transformation and grep for filtering.

See the following code snippet

my @ints = qw(1 2 3 4 5); my @doubleints = map { $_ * 2 } @ints; my @odds = grep { $_ % 2 } @ints; print "INTS = " . join(",", @ints) . "\n"; print "DOUBLE = " . join(",", @doubleints) . "\n"; print "ODDS = " . join(",", @odds) . "\n"; ----------- OUTPUT ------ INTS = 1,2,3,4,5 DOUBLE = 2,4,6,8,10 ODDS = 1,3,5
Update: Thanks to bart for pointing out that I had not closed the italics - used <i> instead of </i> for closing tag.

Mahesh


Comment on Re: map and grep
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2014-12-28 20:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls