www.perl.com
Perl Programming
List Operators
  • map
    • Maps an input list to an output list
    • Powerful, but mapping can be complex
    • $_ is the "fill in the blank" scalar
    • grep is a special case of map

        my @primes = (2, 3, 5, 7, 11, 13, 17, 19);
        my @doubles = map {$_ * 2} @primes;
        print "The doubles of the primes are: @doubles\n";

        my @small = map {$_ < 10 ? $_ : ()} @primes;  # grep {$_ < 10} @primes
        print "The primes smaller than 10 are: @small\n";
      

        The doubles of the primes are: 4 6 10 14 22 26 34 38
        The primes smaller than 10 are: 2 3 5 7
      
Previous http://stuff.mit.edu/iap/perl/