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

Hello again

Is it possible to print two or more arrays in column format?
i.e.

my @array = qw(one two); my @array2 = qw(three four five); my @array3 = qw (six seven); one three six two four seven five
Any ideas?

Replies are listed 'Best First'.
•Re: printing multiple arrays in column format??
by merlyn (Sage) on Feb 07, 2003 at 05:45 UTC
    Finally! One of the things that format does naturally better than anything else in Perl!
    my @array = qw(one two); my @array2 = qw(three four five); my @array3 = qw (six seven); format STDOUT = @<<<<<<<<< @<<<<<<<<< @<<<<<<<<< ~~ shift(@array), shift(@array2), shift(@array3) . write STDOUT;
    {grin}

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: printing multiple arrays in column format??
by jasonk (Parson) on Feb 07, 2003 at 02:42 UTC

    Off the top of my head, I'd probably do something like this:

    while(@array || @array2 || @array3) { printf("%-10s %-10s %-10s\n", shift(@array) || '', shift(@array2) || '', shift(@array3) || '', ); }

    You could do something similar, and iterate from 0 to the last index of the longest array, and just print the element at that index, but that would require you to figure out how long the longest array was as well, but that might be easier if you need to keep the contents of the array around after printing them.

      Thanks jasonk

      That's perfect for what I need :)

Re: printing multiple arrays in column format??
by steves (Curate) on Feb 07, 2003 at 03:23 UTC

    This one is more generalized and doesn't modify the contents of the arrays. It's clunkier than jasonk's but gives you another option if you need it. It could be cleaned up and made more concise I'm sure ...

    use strict; sub array_cols { my $array_ref; my $n = 0; foreach $array_ref (@_) { $n = scalar(@$array_ref) if (scalar(@$array_ref) > $n); } for (my $i = 0; $i < $n; $i++) { foreach $array_ref (@_) { printf "%-12.12s ", $array_ref->[$i]; } print "\n"; } } my @array1 = (qw/one two/); my @array2 = (qw/three four five/); my @array3 = (qw/six seven/); array_cols(\@array1, \@array2, \@array3);
Re: printing multiple arrays in column format??
by BrowserUk (Patriarch) on Feb 07, 2003 at 10:31 UTC

    A very flexible, non-destructive one-liner.

    my @a = qw[one two]; my @b = qw[three four five]; my @c = qw[six seven]; printf +('%-10s 'x3) .$/ , $a[$_]||'', $b[$_]||'', $c[$_]||'' for 0 .. ( sort{ $b<=>$a } ~~@a, ~~@b,~~@c)[0]; one three six two four seven five

    Or generalized as a subroutine

    #! perl -slw use strict; sub pa_columns{ my $fmt = shift; printf( ($fmt x @_).$/ , do { my $n=$_; map{ $_->[$n] || '' } @_; +} ) for 0 .. ( sort{ $b<=>$a } map{ $#$_ } @_ )[0]; } my @a = qw[one two]; my @b = qw[three four five]; my @c = qw[six seven]; pa_columns '%-10s ', \(@a, @b, @c); my @d = 1 .. 10; my @e = 'A' .. 'G'; print $/; pa_columns '%5.5s ', \(@d, @e, @a, @b, @c); __DATA__ C:\test>pa_cols one three six two four seven five 1 A one three six 2 B two four seven 3 C five 4 D 5 E 6 F 7 G 8 9 10

    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.