diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2020-08-28 23:11:57 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2020-08-28 23:11:57 +0200 |
| commit | 6e8b0f83069a80f5cc9f3e36983e139cd125fad2 (patch) | |
| tree | f04fe90c4b684e62b63c63f8a59d642ac0b6bc8d | |
| parent | d2a07299ee402d9d585de3dcf9248c31f0599e07 (diff) | |
| download | perlweeklychallenge-club-6e8b0f83069a80f5cc9f3e36983e139cd125fad2.tar.gz perlweeklychallenge-club-6e8b0f83069a80f5cc9f3e36983e139cd125fad2.tar.bz2 perlweeklychallenge-club-6e8b0f83069a80f5cc9f3e36983e139cd125fad2.zip | |
Fix Perl - printing of histogram if the number is more than 1 digit
| -rw-r--r-- | challenge-075/lubos-kolouch/perl/ch-2.pl | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/challenge-075/lubos-kolouch/perl/ch-2.pl b/challenge-075/lubos-kolouch/perl/ch-2.pl index bbf03742ba..01717a96c6 100644 --- a/challenge-075/lubos-kolouch/perl/ch-2.pl +++ b/challenge-075/lubos-kolouch/perl/ch-2.pl @@ -12,24 +12,28 @@ sub printHistogram { my $hist_max = max(@$histogram); my $out_str; + my $max_len = length($hist_max) + 1; + my $total_len = 0; + for my $i (reverse 1..$hist_max) { - $out_str = $i; + $out_str = sprintf "%${max_len}s", $i; + $total_len += length($i); for my $bar (@$histogram) { - $out_str .= $bar >= $i? '#' : ' ' + $out_str .= $bar >= $i ? sprintf "%${max_len}s", '#' : sprintf "%${max_len}s", ' ' } say $out_str; } - $out_str = '_'; - $out_str .= '_' x scalar @$histogram; + $out_str = '_' x $max_len; + $out_str .= '_' x ($max_len * scalar @$histogram); say $out_str; - $out_str = ' '; - $out_str .= $_ for (@$histogram); + $out_str = ' ' x $max_len; + $out_str .= sprintf "%${max_len}s", $_ for (@$histogram); say $out_str; } |
