I've put 2 spaces between the columns as that's how it looked in your example, but it wasn't specified.

#! perl -sl use strict; use List::Util qw[ max sum ]; use Data::Dumper; our $WIDTH ||= 80; sub columnise { my( $width, @list ) = @_; my @lines; for my $rows ( 1 .. @list ) { my $cols = int( (@list + $rows -1) / $rows ); my @aoa = map{ [ @list[ $_ .. $_+$rows-1 ] ] } map{ $_ * $rows } 0 .. $cols-1; my @widths = map{ 2+ max map length( $_||'' ), @$_ } @aoa; next if sum( @widths ) > $WIDTH; for my $row ( 0 .. $rows-1 ) { push @lines, join '', map{ sprintf "%-$widths[ $_ ]s", $aoa[ $_ ][ $row ]||'' } 0 .. $#aoa; } return @lines; } } chomp( my @list = sort <DATA> ); close DATA; print for columnise $WIDTH, @list; __DATA__ <<SNIP>>

Output at various widths

[20:52:55.75] P:\test>405274 -WIDTH=132 B DB_File Digest Errno Filter IO MIME Op +code SDBM_File Storable Time attrs util ByteLoader Data DynaLoader Fcntl GDBM_File IPC NDBM_File PO +SIX Safe Sys Unicode re Cwd Devel Encode File I18N List ODBM_File Pe +rlIO Socket Thread XS threads [20:46:45.06] P:\test>405274 -WIDTH=80 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 [20:47:05.06] P:\test>405274 -WIDTH=40 B Filter SDBM_File util ByteLoader GDBM_File Safe Cwd I18N Socket DB_File IO Storable Data IPC Sys Devel List Thread Digest MIME Time DynaLoader NDBM_File Unicode Encode ODBM_File XS Errno Opcode attrs Fcntl POSIX re File PerlIO threads [20:47:15.53] P:\test>405274 -WIDTH=20 B util 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 [20:47:23.53] P:\test>405274 -WIDTH=25 B NDBM_File ByteLoader ODBM_File Cwd Opcode DB_File POSIX Data PerlIO Devel SDBM_File Digest Safe DynaLoader Socket Encode Storable Errno Sys Fcntl Thread File Time Filter Unicode GDBM_File XS I18N attrs IO re IPC threads List util MIME

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

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