-
Notifications
You must be signed in to change notification settings - Fork 0
/
cw.pl
executable file
·81 lines (64 loc) · 1.57 KB
/
cw.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/perl
&help unless(scalar @ARGV);
while(@ARGV) {
&process_node(shift)
}
@temp1 = reverse sort by_popularity keys %popularity;
$count = $popularity{$temp1[0]};
print "$count: ";
foreach $key (@temp1) {
if($popularity{$key} != $count) {
$count = $popularity{$key};
$onlyf = 0;
print "\n$count: ";
}
if(++$onlyf == 10) {
print "more...";
} elsif($onlyf < 10) {
print "$key ";
}
}
@temp2 = reverse sort by_paragraph keys %paragraph;
print "\n\nLONGEST PARAGRAPHS\n\n";
$count = 0;
foreach $key (@temp2) {
if($count++ < 10) {
print "$key ", $paragraph{$key}{"occurrence"};
print " ", $paragraph{$key}{"file"}, " ", $paragraph{$key}{"line"},
"\n";
} else {
print "more...";
last;
}
}
sub by_popularity {
return $popularity{$a} <=> $popularity{$b};
}
sub by_paragraph {
return $a <=> $b;
}
sub process_node {
local $temp_name = shift;
local $line = 1;
open(INPUTFILE, $temp_name) || warn "Could not open $temp_name\n";
while(<INPUTFILE>) {
$l = length($_);
if($l > 1) {
++$paragraph{$l}{"occurrence"};
$paragraph{$l}{"file"} = $temp_name;
$paragraph{$l}{"line"} = $line;
}
foreach $val (split(/[\s\'\\\{\}\.\,\;\:\?\!\`\[\]]/)) {
if(length $val) {
++$popularity{lc($val)};
}
}
++$line;
}
close INPUTFILE;
}
sub help {
print "A little tool for counting words\n",
"USAGE: cw.pl file1 file2 ...\n";
exit 0;
}