Tabs would be useful if you could define the tab stops.
A solution:
use List::Util qw( max ); my $max_spam_len = max map length, 'Spam:', @spam; my $format = "%-${max_spam_len}s %s\n"; printf($format, 'Spam:', 'Ham:'); my $max_idx = max $#spam, $#ham; for my $i (0..$max_idx) { printf($format, $i <= $#spam ? $spam[$i] : '', $i <= $#ham ? $ham[$i] : '', ); }
Update: A more generic solution:
use List::Util qw( max ); sub print_columns { my ($data) = @_; my @headers; my @columns; for (my $i = 0; $i < @$data; ) { push(@headers, $data->[$i++]); push(@columns, $data->[$i++]); } my @col_widths = map { max map length, $headers[$_], @{$columns[$_]} } 0..$#headers; my $format = join(' ', map "%-${_}s", @col_widths) . "\n"; my $sep = join(' ', map { '-' x $_ } @col_widths) . "\n"; printf($format, @headers); print($sep); my $max_idx = max map { $#$_ } @columns; for my $i (0..$max_idx) { printf($format, map { $i < @$_ ? $_->[$i] : '' } @columns); } } print_columns([ Spam => \@spam, Ham => \@ham, ]);
Tested.
In reply to Re: Am i formatting correctly? (auto-sizing columns)
by ikegami
in thread Am i formatting correctly?
by dark314
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |