aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2020-10-05 14:44:00 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2020-10-07 21:17:47 +0200
commit90730d8ef9b174c6d736b63be07ebfd17e3632bd (patch)
tree64fb1de4ce97514c5025e269c1c9f81d82066590
parentdd2635a5d37c5a853c5400613eb63c5fc301539c (diff)
downloadperlweeklychallenge-club-90730d8ef9b174c6d736b63be07ebfd17e3632bd.tar.gz
perlweeklychallenge-club-90730d8ef9b174c6d736b63be07ebfd17e3632bd.tar.bz2
perlweeklychallenge-club-90730d8ef9b174c6d736b63be07ebfd17e3632bd.zip
Solution to task 2
-rwxr-xr-xchallenge-081/jo-37/perl/ch-2.pl29
1 files changed, 29 insertions, 0 deletions
diff --git a/challenge-081/jo-37/perl/ch-2.pl b/challenge-081/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..50f80268d7
--- /dev/null
+++ b/challenge-081/jo-37/perl/ch-2.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use List::Util qw(uniqnum pairgrep pairkeys);
+
+# Use the input data provided by the DATA filehandle if no file name
+# is given.
+*ARGV = *DATA{IO} unless @ARGV;
+
+# Override some defaults: line endings, field separator and slurp mode
+local ($\, $,, $/) = ("\n\n", ' ');
+
+# Build a hash of word/frequency pairs from input data. Incorporate
+# specified exceptions into the split expression.
+my %freq;
+$freq{$_}++ foreach split qr{[\."),]*\s+[("]*|--|'s\s+}, <>;
+
+# For each frequency, extract the corresponding words from %freq, sort
+# and print them.
+# Note: "pairkeys" needs to be protected from being interpreted as a
+# comparator sub name by sort.
+print $_, sort +(pairkeys pairgrep {$b == $_} %freq)
+ foreach uniqnum sort {$a <=> $b} values %freq;
+
+__DATA__
+West Side Story
+
+The award-winning adaptation of the classic romantic tragedy "Romeo and Juliet". The feuding families become two warring New York City gangs, the white Jets led by Riff and the Latino Sharks, led by Bernardo. Their hatred escalates to a point where neither can coexist with any form of understanding. But when Riff's best friend (and former Jet) Tony and Bernardo's younger sister Maria meet at a dance, no one can do anything to stop their love. Maria and Tony begin meeting in secret, planning to run away. Then the Sharks and Jets plan a rumble under the highway--whoever wins gains control of the streets. Maria sends Tony to stop it, hoping it can end the violence. It goes terribly wrong, and before the lovers know what's happened, tragedy strikes and doesn't stop until the climactic and heartbreaking ending.