aboutsummaryrefslogtreecommitdiff
path: root/challenge-032/simon-proctor/perl6
diff options
context:
space:
mode:
authorSimon Proctor <simon.proctor@zpg.co.uk>2019-10-28 09:55:51 +0000
committerSimon Proctor <simon.proctor@zpg.co.uk>2019-10-28 09:55:51 +0000
commitb1e98f68aa91f041d7e83034bd9b98a08e6b9ca0 (patch)
tree94a28394148e37f5dc2fad1f75492d4cdfd73bd4 /challenge-032/simon-proctor/perl6
parent4ea534b8afcaa524e258936ac5eae51eaa5a2cc9 (diff)
downloadperlweeklychallenge-club-b1e98f68aa91f041d7e83034bd9b98a08e6b9ca0.tar.gz
perlweeklychallenge-club-b1e98f68aa91f041d7e83034bd9b98a08e6b9ca0.tar.bz2
perlweeklychallenge-club-b1e98f68aa91f041d7e83034bd9b98a08e6b9ca0.zip
Challenge 1 with a few test files
Diffstat (limited to 'challenge-032/simon-proctor/perl6')
-rw-r--r--challenge-032/simon-proctor/perl6/ch-1.p636
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-032/simon-proctor/perl6/ch-1.p6 b/challenge-032/simon-proctor/perl6/ch-1.p6
new file mode 100644
index 0000000000..8253bf56a8
--- /dev/null
+++ b/challenge-032/simon-proctor/perl6/ch-1.p6
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl6
+
+use v6;
+
+subset ValidFile of Str where *.IO.f;
+my %*SUB-MAIN-OPTS = :named-anywhere;
+
+#| Print help text
+multi sub MAIN( Bool :h($help) where so * ) {
+ say $*USAGE;
+}
+
+#| Read date from standard out.
+#| Returns a list of name and count sorted by count
+multi sub MAIN(
+ Bool :$csv = False #= Output in CSV
+) {
+ read-files( IO::CatHandle.new( $*IN ), $csv );
+}
+
+#| Given a list of filenames reads each in turn
+multi sub MAIN(
+ *@files where all(@files) ~~ ValidFile, #= Files to read
+ Bool :$csv = False #= Output in CSV
+) {
+ read-files( IO::CatHandle.new( @files ), $csv );
+}
+
+sub read-files( IO::CatHandle $files, $csv ) {
+ my %results := BagHash.new();
+ %results{$_}++ for $files.lines;
+ my $k-dist = %results.keys.map( *.codes ).max;
+ my $v-dist = %results.values.map( *.codes ).max;
+ my $fmt = $csv ?? '"%s",%d' !! "% -{$k-dist+2}s%{$v-dist+2}d";
+ .say for %results.sort( *.value <=> *.value ).reverse.map( { sprintf($fmt,$_.key,$_.value) } );
+}