parse values while reading lines
authorMischa POSLAWSKY <perl@shiar.org>
Thu, 5 Sep 2019 15:05:39 +0000 (17:05 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Thu, 5 Sep 2019 21:56:49 +0000 (23:56 +0200)
Manipulate input as it comes in, spreading the load and having values ready
for upcoming interrupts without a separate phase to repeat.

graph

diff --git a/graph b/graph
index 501ac3c24f4ce3a86585c785537320ce3eb532a2..cb8a4061c10c5d5bcf68ca9f8154590eb667e0bc 100755 (executable)
--- a/graph
+++ b/graph
@@ -20,16 +20,24 @@ GetOptions(\my %opt,
 $opt{width} ||= $ENV{COLUMNS} || 80;
 $opt{color} //= 1;
 
-my @lines = readline or exit;
-chomp for @lines;
-my @values = map { s/^\h* ( -? [0-9]* (?:\.[0-9]+)? )//x and $1 } @lines;
-my @order  = sort { $b <=> $a } grep { length } @values;
-if (defined $opt{trim}) {
-       my $trimpos = abs $opt{trim};
-       $trimpos <= 1 ? ($_ = substr($_, 0, 1)) :
-       (length > $trimpos and substr($_, $trimpos - 1) = '…') for @lines;
+my (@lines, @values);
+while (readline) {
+       chomp;
+       push @values, s/^\h* ( -? [0-9]* (?:\.[0-9]+)? )//x && $1;
+       if (defined $opt{trim}) {
+               my $trimpos = abs $opt{trim};
+               if ($trimpos <= 1) {
+                       $_ = substr $_, 0, 1;
+               }
+               elsif (length > $trimpos) {
+                       substr($_, $trimpos - 1) = '…';
+               }
+       }
+       push @lines, $_;
 }
+@lines or exit;
 
+my @order  = sort { $b <=> $a } grep { length } @values;
 my $maxval = $order[0];
 my $minval = min $order[-1], 0;
 my $lenval = max map { length } @order;