grep acts a lot like the *nix "grep" command, i.e. it returns the things that match a certain condition. Baseline idea that should get you going is that the condition you pass to grep acts as a filter: grep grabs the things in one list that match some condition or other and puts them into another.
my @filtered = grep { length($_) > 15 } @unfiltered;gives you a list of the strings in @unfiltered that are 16 characters or greater in length.
map is more suited to *transforming* the things in the original list. Suppose I want to uppercase every string in a list, I'd do:
my @uppercased = map { uc($_) } @list_of_strings;
Warning: arrays and lists are not the same thing in Perl, even though my presentation here might suggest that. Moreover, in scalar contexts, these functions will not return lists, but rather scalar values (e.g grep returns the number of things that match the condition)
HTH
Philosophy can be made out of anything. Or less -- Jerry A. Fodor
In reply to Re: Map and Grep
by arturo
in thread Map and Grep
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |