in reply to Re: 'ls -C' column style
in thread 'ls -C' column style

Thanks, this is just the sort of thing I was looking for.

I notice that you treat the 2-char spacer as a "right-padding" on each column rather than as a "between padding" on each pair of columns; this becomes particularly obvious on the -WIDTH=20 example. But I think that would be fairly easy to change, at the cost of a bit of cleanness of the code.

Hugo

Replies are listed 'Best First'.
Re^3: 'ls -C' column style
by BrowserUk (Patriarch) on Nov 06, 2004 at 00:49 UTC

    You've probably done it already, but it suddenly struck me that this fixed the right-padding problem without needing to mess with the code much.

    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{ max map length( $_||'' ), @$_ } @aoa; next if sum( @widths, $#widths * 2 ) > $WIDTH; for my $row ( 0 .. $rows-1 ) { push @lines, join ' ', map{ sprintf "%-$widths[ $_ ]s", $aoa[ $_ ][ $row ]||'' } 0 .. $#aoa; } return @lines; } }

    I also played with your binary chop idea, but the list had to be quite long and the width quite narrow before it offset the extra code required.


    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