Blob


1 #!/usr/bin/perl
3 #Write a program that reads a series of words (with one word per line) until
4 #end-of-input, then prints a summary of how many times each word was seen.
5 #(Hint: remember that when an undefined value is used as if it were a number,
6 #Perl automatically converts it to 0. It may help to look back at the earlier
7 #exercise that kept a running total.) So, if the input words were fred,
8 #barney, fred, dino, wilma, fred (all on separate lines), the output should
9 #tell us that fred was seen 3 times. For extra credit, sort the summary words
10 #in code point order in the output.
12 use warnings;
13 use strict;
14 use utf8;
15 use Data::Dumper;
17 my %count;
18 print "Type some words, with one word per line:\n";
19 chomp(my @words = <>);
20 foreach my $word (@words) {
21 $count{$word}++;
22 }
23 foreach my $word (sort(keys %count)) {
24 print "$word: $count{$word}\n";
25 }