In Perl both grep
and map
take an expression and a list, and evaluate the expression for each element of the list.
What is the difference b
I find that it's helpful to think think about grep()
and map()
in their most general form:
grep {BLOCK} LIST
map {BLOCK} LIST
grep()
is a filter: it returns the subset of items from LIST for which BLOCK returns true.
map()
is a mapping function: send a value from LIST into BLOCK, and BLOCK returns a list of 0 or more values; the combined set of all of those calls to BLOCK will be the ultimate list returned by map()
.