Grep and Map

#!/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";
system("ls -aF | grep /");

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;

@strs = qw(foobar simple four on eighteen);
@two_chars = map {/(..)/g} @strs;
print "@two_chars\n";

@decimals = qw(10 20 30 40 50);
@octals   = map {sprintf "%o", $_} @decimals;
@hexes    = map {sprintf "%x", $_} @decimals;
@squares = map {$_ * $_} @decimals;
print "Octals: @octals\n";
print "Hexes:   @hexes\n";
print "Squares: @squares\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!

$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  ##########################

./     #######  ls -aF output
../
Tk/
cdir/
exams/
garbage/

Directories under current dir: . .. Tk cdir exams garbage

Files > 10000 bytes: hello joke opcode.pl stu tkfaq.targ. gradesw 
                     lab1.s ff 

-rw-------    1 jwp2286  staff      13916 Feb  3 01:01 ff
-rwx------    1 jwp2286  staff      13580 Nov 18 21:58 gradesw
-rwx------    1 jwp2286  staff      13020 Feb 23  1998 hello
-rw-------    1 jwp2286  staff      15728 Feb 23  1998 joke
-rw-------    1 jwp2286  staff      10923 Jan 22 15:22 lab1.s
-rwx--x--x    1 jwp2286  staff      14975 Feb 23  1998 opcode.pl
-rw-------    1 jwp2286  staff      13641 May 20  1998 stu
-rw-------    1 jwp2286  staff      12358 Feb 23  1998 tkfaq.targ.

Filenames >= 20 characters: perms-stat-time.demo inclass.exercise.regexp

Files with >= 300 lines: joke opcode.pl stu lab1.s

Files > 40000 bytes: /mnt/diska/staff/jwp2286/inet/demos/handouts

fo ob ar si mp le fo ur on ei gh te en  ####  Tear strings every 2 bytes.

Octals: 12 24 36 50 62   #####  Original list of decimals (10 20 30 40 50)
Hexes:   a 14 1e 28 32
Squares: 100 400 900 1600 2500

#####  Grep -n emulation.  Matches lines with "print" in code above!!
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";
31   : print "@two_chars\n";
34   : @octals   = map {sprintf "%o", $_} @decimals;
35   : @hexes    = map {sprintf "%x", $_} @decimals;
37   : print "Octals: @octals\n";
38   : print "Hexes:   @hexes\n";
39   : print "Squares: @squares\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!!
58   :      print "(@$listref)\n";

16  ######  Number of lines with "print" in grep -c emulation

(1 3 5)   ######  3-D Coordinates!!
(2 4 9)
(3 5 6)
(4 6 6)