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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
#!/usr/bin/perl
# Create a script that either reads standard input or one or more
# files specified on the command-line. Count the number of times
# and then print a summary, sorted by the count of each entry.
# So with the following input in file example.txt
# apple
# banana
# apple
# cherry
# cherry
# apple
# the script would display something like:
# apple 3
# cherry 2
# banana 1
# For extra credit, add a -csv option to your script, which would generate:
# apple,3
# cherry,2
# banana,1
use strict;
use warnings;
use feature 'say';
my @files;
my $csv_flag = 0;
my $longest = 0;
for my $arg (@ARGV) {
if ($arg eq '-csv') {
$csv_flag = 1;
} else {
push @files, $arg;
}
}
my %dictionary;
if (@ARGV < ($csv_flag+1)) {
say "Type the word(s) then press enter, :end to quit:";
while (chomp(my $word = <STDIN>)) {
($word =~ /^:end$/) && last;
$longest = length $word if $longest < length $word;
$dictionary{$word}++;
}
} else {
for my $text (@files) {
open my $handle, '<', $text;
chomp(my @lines = <$handle>);
close $handle;
for my $word (@lines) {
$longest = length $word if $longest < length $word;
$dictionary{$word}++;
}
}
}
$longest = 0 if $csv_flag;
for my $keys (sort { $dictionary{$b}-$dictionary{$a} } sort keys %dictionary) {
printf ("%-${longest}s%s%s\n",$keys,$csv_flag?",":" ",$dictionary{$keys});
}
=begin
perl .\ch-1.pl
Type the word(s) then press enter, :end to quit:
a
quick
brown
apple
jumps
over
dog
brown
cat
apple
:end
apple 2
brown 2
a 1
cat 1
dog 1
jumps 1
over 1
quick 1
perl .\ch-1.pl .\sample.txt
apple 2
brown 2
dog 2
a 1
banana 1
black 1
cat 1
fox 1
jumps 1
lazy 1
over 1
quick 1
test 1
the 1
perl .\ch-1.pl -csv .\sample.txt
apple,2
brown,2
dog,2
a,1
banana,1
black,1
cat,1
fox,1
jumps,1
lazy,1
over,1
quick,1
test,1
the,1
=cut
|