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

Honourable Monks,
I need some powerful expressions in order to reduce code and avoiding intermediate variables or importing modules.
Suppose an array of strings which are to be printed as a matrix. The array is
my (@b) = qw(un deux trois quatre jedan dva tri tcheteri one two three );
First, how do I find the biggest string length? Does anybody have any command shorter then
my ($max) = sort {$b <=> $a} map length, @a;
Secondly, I want the array printed or displayed in columns as "tight" as possible and _justified left_. I don't manage justifying left with sprintf(). Let's choose 4 columns. Knowing $max, the shortest I manage is:
my $col=4; for(@b){ $_ = substr($_ . ' ' x $max, 0, $max +1)} @a = @b; for (;@a;){print splice (@a, 0, $col),"\n"}
An awful solution because I destroy the array and the aliasing substr($_ . ' ' x $max, 0, $max +1) is horrible. Better solutions are welcome :-) Finally, I want to "transpose" the previous display, or to "shuffle" it. The best I find is
@a = @b; my @c; for (;@a;){ my @b = splice (@a, 0, $col); map {push @{$c[$_]}, $b[$_]; } (0 .. $#b); } map {print @$_,"\n"} @c;
Really disgusting. I want prettier code. The code in one slurp:
use strict; use warnings; my (@a) = my (@b) = qw(un deux trois quatre jedan dva tri tcheteri on +e two three ); print "@a\n"; my ($max) = sort {$b <=> $a} map length, @a; print "max=$max\n"; my $col=4; for(@b){ $_ = substr($_ . ' ' x $max, 0, $max +1)} @a = @b; for (;@a;){print splice (@a, 0, $col),"\n"} print "\ntrasnsposed\n\n"; @a = @b; my @c; for (;@a;){ my @b = splice (@a, 0, $col); map {push @{$c[$_]}, $b[$_]; } (0 .. $#b); } map {print @$_,"\n"} @c;
Thank you for looking at this!

Replies are listed 'Best First'.
Re: Printing an array columnwise or rowwise
by ikegami (Patriarch) on Jul 18, 2006 at 01:16 UTC

    Use a negative width to left-justify. For example,
    "%5s" means pad to 5 characters, right-justified.
    "%-5s" means pad to 5 characters, left-justified.

    The following are not the most compact solutions, but they are probably the most readable. Don't forget that programs should be written for those who maintain them.

    Untransposed:

    use strict; use warnings; use List::Util qw( max ); use POSIX qw( ceil ); my @a = qw(un deux trois quatre jedan dva tritcheteri one two three); my $cols = 4; my $width = (max map length, @a) + 1; my $rows = ceil(@a / $cols); for my $r (0..$rows-1) { for my $c (0..$cols-1) { next if (my $i = $r * $cols + $c) > $#a; printf('%-*s', $width, $a[$i]); } print("\n"); }

    Transposed:

    use strict; use warnings; use List::Util qw( max ); use POSIX qw( ceil ); my @a = qw(un deux trois quatre jedan dva tritcheteri one two three); my $cols = 3; my $width = (max map length, @a) + 1; my $rows = ceil(@a / $cols); for my $r (0..$rows-1) { for my $c (0..$cols-1) { next if (my $i = $r + $c * $rows) > $#a; printf('%-*s', $width, $a[$i]); } print("\n"); }

    Now, wouldn't it be nice if each column was as narrow as possible? There's gotta be a module to do that on CPAN, but I didn't find one quickly.

    Update: I had to leave before I could finish writting my entire node. Here's the rest.

    Update: Fixed a bug in "Transposed". Thanks to jhourcle for pointing it out.

Re: Printing an array columnwise or rowwise
by jhourcle (Prior) on Jul 18, 2006 at 02:09 UTC
    Secondly, I want the array printed or displayed in columns as "tight" as possible and _justified left_. I don't manage justifying left with sprintf(). Let's choose 4 columns. Knowing $max, the shortest I manage is:

    Okay, let's assume we know $max (see ikegami's response) and $col:

    use POSIX qw(ceil); my $rows = ceil( scalar @a / $col ); for ( my $i = 0; $i < $rows; $i++ ) { printf( ("%-${max}s " x $col)."\n", map { defined $_ ? $_ : '' } @a[($i*$col)..(($i*$col)+($col-1))] ); } # transposed $rows = ceil (scalar @a / $col ); for ( my $i = 0; $i < $rows; $i++ ) { printf( ("%-${max}s " x $col)."\n", map { defined $_ ? $_ : '' } @a[ map { $i+($_*$rows) } (0..$col-1) ] ); }

    Of course -- it won't fill the width when transposed -- it fills height first. It'll also extend the end of your array, because of the way I'm messing with it, so you'll want to make a copy first (which isn't too bad if you're working in a subroutine)

    Personally, I'd just like the '#' feature in MudOS's version of (s)printf (you tell it a number of characters wide, and an array, and it generates a listing to fit)

Re: Printing an array columnwise or rowwise
by graff (Chancellor) on Jul 18, 2006 at 02:09 UTC
    You said:
    I don't manage justifying left with sprintf().

    I take that to mean you just haven't learned yet how this is done; ikegami showed how to do this in his initial reply, but he didn't explain (yet). By putting a minus sign immediately after the "%" in the format string, you tell (s)printf to left-justify the printable value within the specified field width (mnemonic: positive field width is right-justified, negative field width is left justified).

    The other trick he demonstrates is the use of "*" instead of a numeric for the field width, which tells (s)printf to take the next available arg in the arg list as the field width.

Re: Printing an array columnwise or rowwise
by logen (Novice) on Jul 18, 2006 at 11:20 UTC
    Thank you all for your kind and helpfull replies. I will be reading the the specified documentation.