returns a new list containing those elements of list for which expression or block produces a true value, whereas
map expression,-or-block list
performs the expression or block for each element of list and returns the list of values the expression produced (i.e. one (*!) for each element supplied in the "input" list).
Both of these update the default variable $_ with the current list element before executing the expression or block. Examples:
(*! Update: unless of course the expression generates more than one or zero values per iteration as johngg points out below)my @sources = grep /\.c$/, glob '*'; # get only the files ending in .c + from a listing of the working directory my @objects = map s/\.c$/\.o/, @sources; # get the list of object file +s that a c compiler would make by default from the files now in @sour +ces my @virgins = map { my $c = $_; $c =~ s/\.o$/\.c/; system "cc $c"; $c } grep !(-f), @objects; # find and compile the uncompiled sources
-M
Free your mind
In reply to Re: map and grep
by Moron
in thread map and grep
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |