in reply to 'ls -C' column style
use strict; my $width = 80; my $spacer = 2; my @files = sort(split(/\s+/, join('', <DATA>))); my ($pl, $c, $i, $j, $max, $total, @max, @sorted); while () { $pl = ($#files + 1) / ++$c; $pl = int $pl + 1 if $pl != int $pl; $total = 0; for ($i = 0; $i < $c; $i++) { $max = 0; for ($j = 0; $j < $pl; $j++) { $max = length($files[$i*$pl+$j]) if $max < length($files[$i*$pl+$j]); } $total += $max + $spacer; } last if ($width < $total - $spacer); } $pl = ($#files + 1) / --$c; $pl = int $pl + 1 if $pl != int $pl; for ($i = 0; $i < $c; $i++) { $max = 0; for ($j = 0; $j < $pl; $j++) { $sorted[$j][$i] = $files[$i*$pl+$j]; $max = length($files[$i*$pl+$j]) if $max < length($files[$i*$pl+$j]); } $max[$i] = $max + $spacer; } $max[$#max] -= $spacer; for (@sorted) { $c = 0; print sprintf('%-'.$max[$c++].'s', $_) for (@$_); print "\n"; } __DATA__ 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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: 'ls -C' column style
by hv (Prior) on Nov 05, 2004 at 11:44 UTC |