获取Perl中按修改日期排序的文件列表 [英] Getting the list of files sorted by modification date in Perl

查看:318
本文介绍了获取Perl中按修改日期排序的文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按修改日期排序的文件列表。我从 根据日期和时间排序目录和列表文件 ,并尝试运行。

I am trying to get the list of files sorted by modification date. I modified the sample program from Sort Directory and list files based on date and time and tried to run it.

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 "$keys\t", scalar localtime($files{$keys}), "\n";
}

我在我的Windows XP 32位机上使用草莓Perl 版本5.12.1.0。

I am running this on my Windows XP 32-bit machine using Strawberry Perl version 5.12.1.0.

Windows上的目录列表是:

The directory listing on Windows is:

输出是:

输出对我来说没有多大意义。这段代码出了什么问题,而且$ code> foreach 是如何循环排列文件列表的呢?

The output doesn't make much sense to me. What is going wrong with this piece of code and how exactly is the foreach loop sorting the list of files?

推荐答案

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

In get_sorted_files, $dir is a glob, not the directory name. Perhaps you meant $path?

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

这篇关于获取Perl中按修改日期排序的文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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