Perl目录,文件排序,带有大小和日期的打印列表 [英] Perl directories, file sort, print list with size and date

查看:115
本文介绍了Perl目录,文件排序,带有大小和日期的打印列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是:

  1. 从命令行读取目录,排序类型和排序顺序.
  2. 对文件名进行排序,并按大小和日期将其打印出来.

这是我到目前为止所得到的.

Here is what I got so far.

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

use Getopt::Long;
my $dir = "";
my $sortby = "";
my $order = "";
my $result;

$result = GetOptions (
                    'dir=s'  => \$dir,                # specify derictory
                   'sortby=s'    =>  \$sortby,        # 'name' or 'date'
                   'order=s'     =>  \$order);        # 'asc'- or 'des'-ending order of sorting

print "derictory = $dir, sortby  = $sortby, order = $order \n\n";


opendir (DH, $dir)or die "couldn open dericroty: $!\n";
my @filenames = grep ! /^\./, readdir DH;
closedir (DH);

if ($sortby eq "name") {

     if ($order eq "asc") {
         foreach my $name (sort {lc $a cmp lc $b} @filenames) {
         my @statinfo = stat("$dir/$name");
         print "$name\tsize= " . $statinfo[7] . ",\t last modified=" . 
         scalar(localtime($statinfo[9])) . "\n";
         }
     }
     elsif ($order eq "des") {
         foreach my $name (sort {lc $b cmp lc $a} @filenames) {
         my @statinfo = stat("$dir/$name");
         print "$name\tsize= " . $statinfo[7] . ",\t last modified=" . 
         scalar(localtime($statinfo[9])) . "\n";
         }
     }      
}


if ($sortby eq "date") {
    if ($order eq "asc") {
        @filenames = sort { -M "$dir/$a" <=> -M "$dir/$b" } (@filenames);   
        print  join ("\n", @filenames);
    }    
    elsif ($order eq "des") {
        @filenames = sort { -M "$dir/$b" <=> -M "$dir/$a" } (@filenames);   
        print  join ("\n", @filenames);
    }    
}

问题是,如果我需要按修改后的日期对它进行排序,我不知道如何打印出带有大小和日期的文件名列表.我想我应该使用stat函数,但是我无法遍历名称并获取每个stat.
我上面所拥有的基本上就是我能够用Google搜集到的内容.

The problem is if I need to sort it by date modified, I don't know how to print out the list of the file names with the size and date. I guess I am supposed to use the stat function, but I can't loop through names, and get each stat.
All I have above is basically what I was able to google and put together.

推荐答案

这是思考问题的另一种方法.要点:

Here's a different way to think about the problem. The essential points:

  1. 编写做简单事情的小功能,并构建您的程序 通过将这些功能组装在一起.

  1. Write small functions that do simple things, and build your program by assembling those functions together.

如果您在便捷数据中收集所有信息 结构(在此示例中为哈希列表),算法/逻辑 该程序的各个方面变得容易和自然.

If you collect all of your information in a convenient data structure (in this example, a list of hashes), the algorithmic/logical aspects of the program become easy and natural.

为简单起见,此示例忽略选项解析,而是仅将参数接受为常规命令行参数.

For simplicity, this example ignore option-parsing and instead just accepts the params as regular command line arguments.

use strict;
use warnings;

main();

sub main {
    my ($dir, $sortby, $order) = @ARGV;

    my @contents = read_dir($dir);
    my $sb       = $sortby eq 'date' ? 'mtime' : 'path';
    my @sorted   = sort { $a->{$sb} cmp $b->{$sb}  } @contents;
    @sorted      = reverse(@sorted) if $order eq 'des';

    for my $fi (@sorted){
        print $fi->{path}, ' : ', $fi->{mtime}, "\n";
    }
}

sub read_dir {
    # Takes a dir path.
    # Returns a list of file_info() hash refs.
    my $d = shift;
    opendir(my $dh, $d) or die $!;
    return map  { file_info($_) }  # Collect info.
           map  { "$d/$_" }        # Attach dir path.
           grep { ! /^\.\.?$/ }    # No dot dirs.
           readdir($dh);
}

sub file_info {
    # Takes a path to a file/dir.
    # Returns hash ref containing the path plus any stat() info you need.
    my $f = shift;
    my @s = stat($f);
    return {
        path  => $f,
        mtime => $s[9],
    };
}

这篇关于Perl目录,文件排序,带有大小和日期的打印列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆