Perl readdir按顺序排列
发布时间:2020-12-15 23:34:43 所属栏目:大数据 来源:网络整理
导读:有没有办法保证readdir返回的列表中的订单? 我有代码: opendir(my $DIR,$src) or die "Error opening $src";# Loop for each file in the directorywhile (my $file = readdir($DIR)){ print "$filen";} 但它以随机顺序返回.现在我知道通过快速谷歌搜索有
有没有办法保证readdir返回的列表中的订单?
我有代码: opendir(my $DIR,$src) or die "Error opening $src"; # Loop for each file in the directory while (my $file = readdir($DIR)) { print "$filen"; } 但它以随机顺序返回.现在我知道通过快速谷歌搜索有很多解决方案,但我找不到我需要的确切订单.. 例如,现在,如果我有文件夹结构: folder folder file1 file2 file3 我得到了结果: file2 folder folder file1 file3 我真的想要的时候: folder folder file1 file2 file3 要么: file1 file2 file3 folder folder 有没有办法实现这个目标? 谢谢. 解决方法
您可以先放置文件夹,然后按文件/目录名称排序,
# $src pointing to folder open with opendir my @sorted_dir = map $_->[0],sort { $a->[1] <=> $b->[1] || $a->[0] cmp $b->[0] } map [ $_,-f "$src/$_" ],readdir($DIR); 虽然可以实现类似的效果, for my $file (sort { -f "$src/$a" <=> -f "$src/$b" } readdir($DIR)) { print "$filen"; } 它的速度较慢且效率较低,因为如果目录条目是普通文件,它通常会进行文件系统检查. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |