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!


In reply to Re: 'ls -C' column style by ihb
in thread 'ls -C' column style by hv

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.