加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

获取按Perl中的修改日期排序的文件列表

发布时间:2020-12-15 23:28:22 所属栏目:大数据 来源:网络整理
导读:我试图获取按修改日期排序的文件列表.我从 Sort Directory and list files based on date and time 修改了示例程序并尝试运行它. sub get_sorted_files { my $path = shift; opendir my($dir),$path or die "can't opendir $path: $!"; my %hash = map {$_ =
我试图获取按修改日期排序的文件列表.我从 Sort Directory and list files based on date and time 修改了示例程序并尝试运行它.

sub get_sorted_files {
    my $path = shift;
    opendir my($dir),$path or die "can't opendir $path: $!";
    my %hash = map {$_ => (stat($_))[9]}
               map  { "$dir$_" }
               grep { m/.*/i }
               readdir $dir;
    closedir $dir;
    return %hash;
}

my %files = get_sorted_files(".");
foreach my $keys (sort{$files{$a} <=> $files{$b}} keys %files) {
    print "$keyst",scalar localtime($files{$keys}),"n";
}

我使用Strawberry Perl版本5.12.1.0在我的Windows XP 32位机器上运行它.

Windows上的目录列表是:

输出是:

输出对我来说没有多大意义.这段代码出了什么问题,foreach循环如何对文件列表进行排序?

解决方法

在get_sorted_files中,$dir是一个glob,而不是目录名.也许你的意思是$path?

my %hash = map {$_ => (stat($_))[9]}
           map  { "$path/$_" }              # $path,not $dir
           grep { m/.*/i }
           readdir $dir;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读