From 8e5ab06d01ae253baa5be620c3dcb0996985587c Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Wed, 4 Sep 2019 15:16:23 +0200 Subject: [PATCH] write out script, enable warnings Mostly the same code but more readable and maintainable. Integers are captured and bars enlarged to fill screen width. --- graph | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/graph b/graph index fbc1248..d20fcd3 100755 --- 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); +} -- 2.30.0