-
Notifications
You must be signed in to change notification settings - Fork 6
/
numbers2histogram
executable file
·75 lines (61 loc) · 1.57 KB
/
numbers2histogram
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/perl
# This script converts csv with 2 columns into a historgram
#
# key1,number
# key1,number
# key2,number
# key2,numner
use utf8;
use strict;
use POSIX;
use List::Util qw[min max];
use Term::ReadKey;
my @size = Term::ReadKey::GetTerminalSize STDOUT;
my $columns = $size[0];
my $leftcols = 15; # Width of left 2 cols
my $barwidth = $columns - $leftcols;
my $scale = 1;
my $key; # value of first col
my $count = 0; # how many values we have counted with the same col1
my $total = 0; # running total of all values
my $min = 10000; #
my $max = 0; #
my $lastkey = ''; #
my $v;
foreach my $line (<>){
chomp $line;
($key,$v) = split /,/, $line;
if ($key ne $lastkey) {
print draw_hist($scale, $lastkey, $count, $min, $max);
$count = 0;
$total = 0;
$max = 0;
$min = 100000;
}
$min = min($min,$v);
$max = max($max,$v);
$count++;
$total += $v;
$lastkey = $key;
}
print draw_hist($scale, $lastkey, $count, $min, $max);
sub draw_hist {
my ($scale, $key, $count, $min, $max) = @_;
if ($count < 1 or $total == 0){
return;
}
my $avg = sprintf("%.3f", $total / $count);
$min = floor($min * $scale);
$avg = floor($avg * $scale);
$max = ceil($max * $scale);
my $r = '';
$r .= sprintf("%-".$leftcols.'s', "$key,$count,");
$r .= sprintf("%5s ", $min);
$r .= (" " x $min);
$r .= "[";
$r .= ("-" x ($avg-$min-1));
$r .= "|";
$r .= ("-" x ($max-$avg-1));
$r .= "] $max";
$r .= "\n";
}