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
Think of grep as map with a filter. map iterates and provides an opportunity to do something with every item. For example these two lines are equivalent:
my @copy = @original;
my @copy = map {$_} @original;
Similarly, these two are equivalent:
my @copy = grep {-f $_} @original;
@copy = ();
for (@original)
{
push @copy, $_ if -f $_;
}
grep provides the ability to insert a conditional, and therefore becomes a filter.