【发布时间】:2011-12-19 01:44:34
【问题描述】:
我将如何对以下代码的多列进行排序?
目前,代码:
1. 获取$directory 中的文件的@list
2.使用正则表达式获取@list中每个元素的$fileName、$fileLocation和$fileSize
3. 将 (2) 中的 3 个值打印到 3 个固定宽度的列中
4.然后打印出文件总数和目录大小
我希望输出按以下顺序显示:
1.$fileName然后
2.$fileLocation然后
3.$fileSize
$directory = '/shared/tmp';
$count = 0;
@list = qx{du -ahc $directory};
printf ("%-60s %-140s %-5s\n", "Filename", "Location", "Size");
foreach(@list) {
chop($_); # remove newline at end
if (/^(.+?K)\s+(.+\/)(.+\.[A-Za-z0-9]{2,4})$/) { # store lines with valid filename into new array
# push(@files,$1);
$fileSize = $1;
$fileLocation = $2;
$fileName = $3;
if ($fileName =~ /^\./) {
next; }
printf ("%-60s %-140s %-5s\n", $fileName, $fileLocation, $fileSize);
$count++;
}
else {
next;
}
}
print "Total number of files: $count\n";
$total = "$list[$#list]";
$total =~ s/^(.+?)\s.+/$1/;
print "Total directory size: $total\n";
【问题讨论】: