Fixed division by 0 if run with a file argument.

This commit is contained in:
Ole Tange 2014-08-05 00:24:16 +02:00
parent 9045bfe6a4
commit 583d6970f9
2 changed files with 17 additions and 47 deletions

View file

@ -1,44 +0,0 @@
--input h,v
foo,2
bar,4
--input hv
foo 2
bar 4
--input ihv
ignore foo 2
ignore bar 4
--format hbv
foo ====== 2
bar ============ 4
--format HbV
=foo=2=
=bar=========4=
--format hb
foo ======
bar ============
--format hb%
foo ====== 50%
bar ============ 100%
--format hbv%
foo ====== 2 50%
bar ============ 4 100%
--format hVb%
foo =2===== 50%
bar =4=========== 100%

View file

@ -381,12 +381,12 @@ if($#ARGV != -1) {
my ($max_value_length, $max_header_length, $max_value_header_length, $header_ref, $value_ref); my ($max_value_length, $max_header_length, $max_value_header_length, $header_ref, $value_ref);
if(not defined $opt::input) { if(not defined $opt::input) {
my $delimiter = guess_delimiter(@raw); my $delimiter;
if($opt::delimiter) { if($opt::delimiter) {
# override guessed delimiter if given # override guessed delimiter if given
$delimiter = $opt::delimiter; $delimiter = $opt::delimiter;
} else { } else {
# Guess opt::input # Guess opt::delimiter
$delimiter = guess_delimiter(@raw); $delimiter = guess_delimiter(@raw);
} }
if(defined $delimiter) { if(defined $delimiter) {
@ -413,6 +413,8 @@ if(not defined $opt::input) {
} }
my $max_value = undef_as_zero(max(@$value_ref)); my $max_value = undef_as_zero(max(@$value_ref));
my $total_value = undef_as_zero(sum(@$value_ref)); my $total_value = undef_as_zero(sum(@$value_ref));
$max_value ||= 1;
$total_value ||= 1;
sub parse_raw_given_opt_input { sub parse_raw_given_opt_input {
my ($input,@raw) = @_; my ($input,@raw) = @_;
@ -633,7 +635,19 @@ sub guess_delimiter {
/[a-zA-Z0-9]/ and next; /[a-zA-Z0-9]/ and next;
$charcount{$_}++ $charcount{$_}++
} }
my $guess = (sort { $charcount{$b} <=> $charcount{$a} } keys %charcount)[0]; my $guess;
for my $g (
# Force trying these first:
"\0", "\t", ";", ",", " ",
sort { $charcount{$b} <=> $charcount{$a} } keys %charcount) {
# The delimiter must be found in every single line
if(grep !/\Q$g\E/, @raw) {
next;
}
$guess = $g;
last;
}
if(defined $guess and $guess =~ /\s/) { if(defined $guess and $guess =~ /\s/) {
# If the guess is a white space, then use 1+ whitespaces # If the guess is a white space, then use 1+ whitespaces
$guess = '\s+'; $guess = '\s+';