aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoelle Maslak <jmaslak@antelope.net>2019-10-29 11:39:27 -0500
committerJoelle Maslak <jmaslak@antelope.net>2019-10-29 11:39:27 -0500
commitecbeda9b573c588eec9cf7f43a83844c87cf127b (patch)
treecc2abf597bc3f0aa214a47df3dc8beb3e5087139
parent75b1f01a47600d81e6a553946a9442da8ebfb8f7 (diff)
downloadperlweeklychallenge-club-ecbeda9b573c588eec9cf7f43a83844c87cf127b.tar.gz
perlweeklychallenge-club-ecbeda9b573c588eec9cf7f43a83844c87cf127b.tar.bz2
perlweeklychallenge-club-ecbeda9b573c588eec9cf7f43a83844c87cf127b.zip
Joelle's Solution to 32.2 in Perl 5
-rwxr-xr-xchallenge-032/joelle-maslak/perl5/ch-2.pl54
1 files changed, 54 insertions, 0 deletions
diff --git a/challenge-032/joelle-maslak/perl5/ch-2.pl b/challenge-032/joelle-maslak/perl5/ch-2.pl
new file mode 100755
index 0000000000..5242b69ae9
--- /dev/null
+++ b/challenge-032/joelle-maslak/perl5/ch-2.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/env perl
+use v5.24;
+use strict;
+use warnings;
+
+# Turn on method signatures
+use feature 'signatures';
+no warnings 'experimental::signatures';
+
+use autodie;
+use Getopt::Long;
+use List::Util qw(max min);
+
+MAIN: {
+ my $csv;
+ my $screen_width = 80;
+ my $base;
+ my $deliminator = "\t";
+
+ GetOptions(
+ "csv" => \$csv,
+ "screen-width=i" => \$screen_width,
+ "base=i" => \$base,
+ "deliminator=s" => \$deliminator,
+ );
+
+ my @words;
+ while (my $lines = <<>>) {
+ chomp $lines;
+ my (@parts) = split "$deliminator", $lines;
+
+ die "All lines must have one and only one deliminator" unless scalar(@parts) == 2;
+
+ push @words, \@parts;
+ }
+
+ my $maxlen = max map { length $_->[0] } @words;
+ my $maxval = max map { $_->[1] } @words;
+ my $minval = min map { $_->[1] } @words;
+
+ $base //= $minval;
+ die "Base should be less or equal to minimum value" unless $base <= $minval;
+
+ my $spread = $maxval - $base; # How far apart are max and min?
+ my $maxbar = $screen_width - $maxlen - 4; # How big the bar can be, we don't use the last column
+ my $unitsize = $maxbar ? ($spread / $maxbar) : 0; # What a '#' represents
+
+ foreach my $ele (@words) {
+ my $hashes = int(($ele->[1] - $base) / $unitsize);
+ $hashes = $maxbar if $unitsize == 0;
+
+ say sprintf("%-${maxlen}s", $ele->[0]) . " | " . ("#" x $hashes);
+ }
+}