diff options
| author | Walt Mankowski <waltman@pobox.com> | 2020-10-06 19:57:20 -0400 |
|---|---|---|
| committer | Walt Mankowski <waltman@pobox.com> | 2020-10-06 19:57:20 -0400 |
| commit | fb9bee1b7c989e55ca10b43bc2553700f558aabd (patch) | |
| tree | 0499b08d08907acb0d90380e97530157d516880f | |
| parent | 6c58dba68670dfc7c703bb4a39455196eb3dbd97 (diff) | |
| download | perlweeklychallenge-club-fb9bee1b7c989e55ca10b43bc2553700f558aabd.tar.gz perlweeklychallenge-club-fb9bee1b7c989e55ca10b43bc2553700f558aabd.tar.bz2 perlweeklychallenge-club-fb9bee1b7c989e55ca10b43bc2553700f558aabd.zip | |
perl code for challenge 81 task 2
| -rw-r--r-- | challenge-081/walt-mankowski/perl/ch-2.pl | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/challenge-081/walt-mankowski/perl/ch-2.pl b/challenge-081/walt-mankowski/perl/ch-2.pl new file mode 100644 index 0000000000..68e2900945 --- /dev/null +++ b/challenge-081/walt-mankowski/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw(:5.32); +use experimental qw(signatures); + +# You are given file named input. +# +# Write a script to find the frequency of all the words. +# +# It should print the result as first column of each line should be +# the frequency of the the word followed by all the words of that +# frequency arranged in lexicographical order. Also sort the words in +# the ascending order of frequency. +# +# For the sake of this task, please ignore the following in the input file: +# +# . " ( ) , 's -- + +my %h; + +while (<>) { + chomp; + s/--/ /; + my @tok = split / /; + for my $tok (@tok) { + $tok =~ s/[."(),]//g; + $tok =~ s/'s$//; + $h{$tok}++; + } +} + +my $last = 0; +for my $k (sort { $h{$a} <=> $h{$b} || $a cmp $b } keys %h) { + if ($h{$k} != $last) { + print "\n" unless $last == 0; + print $h{$k}; + $last = $h{$k}; + } + print " $k"; +} + +print "\n"; + |
