in reply to 'ls -C' column style

I used a different approach than most other replyers in the thread. Instead of so much matrix stuff I've abstracted away the ugly parts. Also, there's a clear separation between data structure creation and output formatting and output. This is less efficient, but it reads well IMHO and this is the style in which I prefer to code. It's easier for me to have refactored code like this. I, again, used my utility module ihb::List::Util and imported &group and &transpose, and produced the following code. (&transpose should be fairly obvious. &group is explained here).

use ihb::List::Util qw/ group transpose /; use List::Util qw/ max sum /; my @files = sort qw/ B Digest Filter MIME SDBM_File Time +util ByteLoader DynaLoader GDBM_File NDBM_File Safe Unicode Cwd Encode I18N ODBM_File Socket XS DB_File Errno IO Opcode Storable attrs Data Fcntl IPC POSIX Sys re Devel File List PerlIO Thread threads /; my $width = 55; my $padding = 2; # Columnize my (@cols, @widths); for (my $rows = 1; 1; $rows++) { @cols = group($rows, @files); @{$cols[-1]} = grep defined, @{$cols[-1]}; @widths = map max(map length, @$_), @cols; my $tw = sum(@widths) + $padding * (@widths - 1); last if $tw < $width or $rows == @files; } # Format columns for (0 .. $#cols) { my $format = "%-$widths[$_]s"; for (@{$cols[$_]}) { $_ = sprintf $format, $_; } } # Output rows { local $" = ' ' x $padding; print "@$_\n" for transpose(@cols); }

In the OP it's not clear how newlines should be handled if the outputted string is equal to the length, also, it's not clear how filenames longer than the width should be handled, so a newline is always appended.

ihb

See perltoc if you don't know which perldoc to read!
Read argumentation in its context!