-
Notifications
You must be signed in to change notification settings - Fork 319
/
ch-2.pl
executable file
·54 lines (42 loc) · 1.4 KB
/
ch-2.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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);
}
}