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, ]);