aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-075/lubos-kolouch/perl/ch-2.pl16
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;
}