in reply to Printing an array columnwise or rowwise

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)