Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: do not understand grep example

by moritz (Cardinal)
on Jun 15, 2012 at 17:29 UTC ( #976466=note: print w/ replies, xml ) Need Help??


in reply to do not understand grep example

thought maybe it calls the subroutine with the entire list (even though this is not how grep is supposed to work),

No, it does not. It calls the subroutine for every value in the list, and only returns if the subroutine returns a true value.

So you could also write

my @odd_digit_sum = grep digit_sum_is_odd($_), @input numbers;

As

my @odd_digit_sum; for (@input) { if (digit_sum_is_odd($_)) { push @odd_digit_sum, $_; } }

and it would return the same result. That's what grep does: it goes through the list, calls the function for every value, and collect those values for which the function returned something true.


Comment on Re: do not understand grep example
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2013-05-23 05:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best material for plates (tableware) is:









    Results (473 votes), past polls