This one loops over the list of items once (at most). It computes the widths for all possible numbers of columns as it goes, eliminating cases of too-many columns when it determines that they would be too wide.

Fully tested and now it does not waste time on column counts over about @items/2 and (update 3) chooses single-row output correctly.

#!/usr/bin/perl -w use strict; my( $maxWid )= ( @ARGV, 79 ); my @items = qw( B ByteLoader Cwd DB_File Data Devel Digest DynaLoader Encode Errno Fcntl File Filter GDBM_File I18N IO IPC List MIME NDBM_File ODBM_File Opcode POSIX PerlIO SDBM_File Safe Socket Storable Sys Thread Time Unicode XS attrs re threads util ); print '='x$maxWid, $/; print Columns( $maxWid, \@items ); exit( 0 ); sub Columns { my( $maxWid, $avItems )= @_; my $maxCols= 1 + $#$avItems/2; my @height= ( 0, map 1+int($#$avItems/$_), 1..$maxCols ); my @total= ( $#$avItems, 0 .. ($maxCols-1) ); my @width; for my $i ( 0..$#$avItems ) { my $len = length( $avItems->[$i] ); $total[0] += $len; for my $cols ( 1 .. $maxCols ) { for( $width[ $cols ][ $i/$height[$cols] ] ) { $_ ||= 0; if( $_ <= $len ) { $total[$cols] += $len - $_; if( $maxWid < $total[$cols] ) { $maxCols= $cols - 1; } $_= $len; } } last if $maxCols < $cols; } last if $maxCols < 2; } $maxCols ||= 1; my $height= $height[$maxCols]; @width= @{ $width[$maxCols] }; if( $total[0] <= $maxWid ) { $maxCols= @$avItems; $height= 1; @width= (0) x $maxCols; } my $text= ''; for my $l ( 1 .. $height ) { my $i= $l - 1; my $c= 0; while( $i < @$avItems ) { my $item= $avItems->[$i]; $i += $height; if( $i < @$avItems ) { $text .= sprintf "%-$width[$c++]s ", $item; } else { $text .= $item . $/; } } } return $text; }

- tye        


In reply to Re: 'ls -C' column style (sideways) by tye
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.