dark314 has asked for the wisdom of the Perl Monks concerning the following question:

-----------------UPDATE---------------
I ended up using printf in the following manner, thanks for your replies:
Note: the -10 indicates to align-left and with 10 places.
foreach (@unseen) { printf ("%-10s\t\t%-10s\n",$seen[$i],$unseen[$i]); $i--; }
---------------------------UPDATE ABOVE-----------------
I'm trying to have the output neatly in two columns but for some reason I am doing something wrong, because when there are no more files on the left side, the formatting screws up!
print "Email folder: $EMAIL_FLDR\n"; print "Spam:\t\tHam:\n"; foreach (@unseen) { print "$seen[$i]\t\t$unseen[$i]\n"; $i--; }
Small snippet of output: (notice the last two files)
Spam: Ham: ./50257_S ./36967_X ./27649_S ./mailtest.pl~ ./32894_S ./mailtest.pl ./71114_S ./rules ./54615_S ./22096_X ./41237_S ./56628_X ./1902_S ./2780_X ./50257_S ./87952_X ./90737_X ./51698_X

Replies are listed 'Best First'.
Re: Am i formatting correctly?
by Fletch (Bishop) on Aug 14, 2006 at 18:13 UTC

    TAB characters indent relative to the current position. Since the last two rows of your data have nothing in the first column the tabs don't indent over as far as those in the prior rows which do.

    You need to look at either using sprintf to pad out everything in each column to a fixed width (even undef or empty values), or use something like Perl6::Form (or the older perlform).

Re: Am i formatting correctly? (auto-sizing columns)
by ikegami (Patriarch) on Aug 14, 2006 at 18:54 UTC

    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.

Re: Am i formatting correctly?
by ady (Deacon) on Aug 14, 2006 at 18:19 UTC
    In the last 2 lines we may assume that
    $seen[$i] is equal to "" (the empty string)
    That would explain the formating, and indicate a path for a solution.
    best regards / allan