write out script, enable warnings
authorMischa POSLAWSKY <perl@shiar.org>
Wed, 4 Sep 2019 13:16:23 +0000 (15:16 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Thu, 5 Sep 2019 03:11:33 +0000 (05:11 +0200)
Mostly the same code but more readable and maintainable.
Integers are captured and bars enlarged to fill screen width.

graph

diff --git a/graph b/graph
index fbc12487fda295ae3a15de72f809480c84867994..d20fcd303750d4bd94be366e6e8bdee151c898c4 100755 (executable)
--- a/graph
+++ b/graph
@@ -1,6 +1,18 @@
-#!/usr/bin/perl -l
-use List::Util 'max';
-chomp for my @r = <>;
-my $len = 1 + max map length, @r;
-my $size = (80 - $len) / max @r;
-print $_, ' 'x($len-length), '#'x($_*$size) for @r;
+#!/usr/bin/env perl
+use 5.014;
+use warnings;
+use List::Util qw( max );
+
+my $width = $ENV{COLUMNS} || 80;
+
+my @lines = readline;
+chomp for @lines;
+my @values = map { /^\h*([0-9]*)/ } @lines;
+my $maxval = max @values;
+my $len    = 1 + max map { length } @lines;  # left padding
+my $size   = ($width - $len) / $maxval;  # bar multiplication
+
+for (@lines) {
+       my $val = shift @values;
+       say $_, ' ' x ($len - length), '#' x ($val * $size);
+}