如何针对以下代码对多列进行排序?
目前,代码:
1.获取@list
中的$directory
个文件
2.使用正则表达式获取$fileName
中每个元素的$fileLocation
,$fileSize
和@list
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";