From ora.com!spdcc!think.com!yale.edu!qt.cs.utexas.edu!cs.utexas.edu!sun-barr!sh.wide!wnoc-tyo-news!sranha!sranhd!sran230!utashiro Sun Nov 15 08:38:25 1992
Path: minya!ora.com!spdcc!think.com!yale.edu!qt.cs.utexas.edu!cs.utexas.edu!sun-barr!sh.wide!wnoc-tyo-news!sranha!sranhd!sran230!utashiro
From: utashiro@sran230.sra.co.jp (Kazumasa Utashiro)
Newsgroups: comp.lang.perl
Subject: Re: an ls -C Perl function
Keywords: perl
Message-ID: <BxrF83.Hqu@sran230.sra.co.jp>
Date: 15 Nov 92 13:38:25 GMT
References: <1992Nov13.141515@se28.wg2.waii.com>
Organization: Software Research Associates, Inc., Japan
Lines: 26

In article <1992Nov13.141515@se28.wg2.waii.com> starr@wg2.waii.com writes:
>> Here is a function to take a list of file names, and sort it as an
>> ls -C does (sorted down columns).

Here is my function.

;#
;# implement ls -C
;#
sub column_out {
    local(@item) = @_;
    local($[, $i, $maxlen, $w, $c, $l) = (0, 0, 8);

    return unless @item;
    for (@item) { $w = (length() + 1 + 7) & ~7 if length() > $w - 1; } # width
    $c = $w >= 80 ? 1 : int(80 / $w);	# column
    $l = int((@item + $c - 1) / $c);	# line
    $c-- while $l * $c - @item + 1 > $l;

    for (@item[sort {$a % $l <=> $b % $l || $a <=> $b} $[ .. $#item]) {
	print;
	print $i == $#item || ++$i % $c == 0 ? "\n" : ' ' x ($w - length);
    }
}

--utashiro


