The Grep and Map Functions
#!/usr/bin/perl -w
############################ Grep and Map #############################
opendir(D,".") or die "Cannot open current directory!\n";
@dirs = grep { -d } readdir D;
print "Directories under current dir: @dirs\n";
rewinddir D;
@bigger_than_10000_bytes = grep { -s > 10000 } readdir D;
print "Files > 10000 bytes: @bigger_than_10000_bytes\n";
rewinddir D;
@longnames = grep { length() >= 20} readdir D;
print "Filenames >= 20 characters: @longnames\n";
rewinddir D;
@three_hundred_lines = grep {open(F,"$_"),@lines = <F>,@lines >= 300} readdir D;
print "Files with >= 300 lines: @three_hundred_lines\n";
$dir = "$ENV{HOME}/inet/demos";
opendir(D, "$dir") or die "Cannot open internet demos!\n";
@bigger_than_40000_bytes = grep { -s > 40000 } map {"$dir/$_"} readdir D;
print "Files > 40000 bytes: @bigger_than_40000_bytes\n";
closedir D;
@decimals = qw(10 20 30 40 50);
@octals = map {sprintf "%o", $_} @decimals; # Three one-to-one mappings!
@hexes = map {sprintf "%x", $_} @decimals;
@squares = map {$_ * $_} @decimals;
print "Octals: @octals\n";
print "Hexes: @hexes\n";
print "Squares: @squares\n";
@strs = qw(foobar simple four on eighteen);
@two_chars = map {/../g} @strs; # One-to-many mapping!
print "@two_chars\n";
# grep -n emulation
open(F,"grep-map.pl") or die "Cannot open grep-map.pl!\n";
@matches = map {$lnum++; /print/ ? sprintf("%-5d: %s", $lnum, $_) : ()} <F>;
print @matches; # () takes up no element in @matches!!!
# grep -c emulation
seek(F, 0, 0);
$count = grep {/print/} <F>;
print "$count\n"; # Number of lines matching the pattern!!
# Grep has a meaningful scalar context. Map doesn't!
@scores = qw(75 65 63 80 97 87 67 76 97 97 84 90);
$high = (sort {$a <=> $b} @scores)[-1];
$NumHighs = grep {$_ == $high} @scores; # How many people had the high score?
print "$NumHighs\n";
%scores = ("Doe,John" => 75, "Dover,Ben" => 65, "Wonka,Willie" => 63,
"He,Ben" => 80, "Smart,Tom" => 97, "Smith,Chris" => 67,
"Tal,Mike" => 76, "Yang,Yu" => 97, "Sheth,Mia" => 97,
"Horn,Joe" => 84, "Larkin,Ed" => 90);
$HighScore = (sort {$a <=> $b} values %scores) [-1];
%Highs = map {$scores{$_} == $HighScore ? ($_, $scores{$_}) : ()} keys %scores;
foreach $name (keys %Highs) {print "$name $Highs{$name}\n"}
$x = [1,2,3,4]; # A taste of the future ---> REFERENCES!!
$y = [3,4,5,6];
$z = [5,9,6,6];
@coordinates = map { [$x->[$_], $y->[$_], $z->[$_]] } 0..3;
foreach $listref (@coordinates)
{
print "(@$listref)\n";
}
############################### Program Output ##############################
Directories under current dir: . ..
Files > 10000 bytes: .. bundle file.demo.16
Filenames >= 20 characters: index-substr.demo.18 random-access.demo.17
Files with >= 300 lines: bundle file.demo.16
Files > 40000 bytes: /mnt/diska/staff/perry/inet/demos/handouts
Octals: 12 24 36 50 62 # One-to-one mappings
Hexes: a 14 1e 28 32
Squares: 100 400 900 1600 2500
fo ob ar si mp le fo ur on ei gh te en # One-to-many mappings via m/../g
# grep -n emulation
8 : print "Directories under current dir: @dirs\n";
13 : print "Files > 10000 bytes: @bigger_than_10000_bytes\n";
17 : print "Filenames >= 20 characters: @longnames\n";
21 : print "Files with >= 300 lines: @three_hundred_lines\n";
26 : print "Files > 40000 bytes: @bigger_than_40000_bytes\n";
30 : @octals = map {sprintf "%o", $_} @decimals; # Three one-to-one mappings!
31 : @hexes = map {sprintf "%x", $_} @decimals;
33 : print "Octals: @octals\n";
34 : print "Hexes: @hexes\n";
35 : print "Squares: @squares\n";
39 : print "@two_chars\n";
43 : @matches = map {$lnum++; /print/ ? sprintf("%-5d: %s", $lnum, $_) : ()} <F>;
44 : print @matches; # () takes up no element in @matches!!!
48 : $count = grep {/print/} <F>;
49 : print "$count\n"; # Number of lines matching the pattern!!
55 : print "$NumHighs\n";
64 : foreach $name (keys %Highs) {print "$name $Highs{$name}\n"}
72 : print "(@$listref)\n";
18 # Result of grep -c emulation
3 # Number of people with high score of 97
Sheth,Mia 97 # The hash of those people created via map.
Smart,Tom 97
Yang,Yu 97
(1 3 5) # Result of map used to create [x,y,z] triplets
(2 4 9)
(3 5 6)
(4 6 6)