-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.pl
executable file
·56 lines (47 loc) · 1.04 KB
/
set.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/perl -l
sub readall {
my ($fname) = @_;
open my $handle, '<', $fname;
chomp(my @lines = <$handle>);
close $handle;
return \@lines
}
sub hashify {
my %E = ();
$E{$_} = 1 for @_;
return %E;
}
$usage = sub {
print "Usage: ";
print "$0 file1 + file2 # Union of file contents";
print "$0 file1 - file2 # contents in file1 but not in file2";
print "$0 file1 = file2 # contents in both file1 and file2";
return "";
};
$union = sub {
my ($A, $B) = @_;
my %E = &hashify(@$A, @$B);
return sort keys %E;
};
$subtract = sub {
my ($A, $B) = @_;
my %E = ();
my %EA = &hashify (@$A);
my %EB = &hashify (@$B);
for (keys %EA) {
$E{$_} = 1 unless $EB{$_} ;
}
return sort keys %E;
};
$intersection = sub {
my ($A, $B) = @_;
my @AminusB = $subtract->($A, $B);
return $subtract->($A, \@AminusB)
};
$A = &readall($ARGV[0]);
$O = $ARGV[1];
$B = &readall(<$ARGV[2]>);
%funcs = ( '+' => $union, '-' => $subtract, '=' => $intersection);
$func = $funcs{$O} || $usage;
@result = $func->($A, $B);
print $_ for @result;