Basic HTML version of Foils prepared Sept 20 97

Foil 51 The map and grep functions

From Perl5 and Advanced Perl4 Features Computational Science for Information Age Course CPS616 -- Sept 20 97. by Geoffrey C. Fox,Nancy McCracken,Tom Scavo


1 @chars = map( expr , @nums);
2 Here expr is some Expression or Block which accesses variable $_
3 map sets $_ to be succesive values in list @nums and returns the successive results of executing BLOCK expr with each value of $_
  • These results are returned in list context and may give zero one or more entries into total list @chars
4 A similar construct is grep which acts like map but returns a list containing just the entries in @nums for which expr is TRUE
  • This is clearly like UNIX grep as
  • grep ( /regexp/, @listoflines ); # returns just those lines matching regexp
  • $_ is set by reference to $listoflines[0...] and so altering $_ will alter original @listoflines

in Table To:


© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Sun Apr 5 1998