跨多列排序(Perl)

时间:2011-12-19 01:44:34

标签: perl sorting

如何针对以下代码对多列进行排序?

目前,代码:
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";

0 个答案:

没有答案