小编sup*_*fry的帖子

使用Perl对目录文件名进行排序

我想按名称对目录中的文件进行排序/打印.我的代码列出了所有代码,但排序是偏斜的.这是我的代码和结果.任何建议都将受到欢迎!

my $file;
opendir (DIR, "$dir");
while ($file = readdir(DIR)) {
    push (my @files, $file);
    @files = sort {$a cmp $b} @files;   #NOT sorting!
    foreach $file (@files) {
        print "$file\n";
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是"排序"结果:

Screenshot-Chess_-_Human_versus_GNUchess.png  
test.html  
katyperry.gif  
test.cgi  
Californication.S04E05.HDTV.XviD-ASAP.avi  
FreeWatch_13.exe  
proxy.jpg  
test.pl-  
.  
attachment2.jpg  
attachment.jpg  
Californication.S04E06.HDTV.XviD-LOL.avi  
Californication.S04E07.HDTV.XviD-LOL.avi  
boxter.jpg  
..  
Run Code Online (Sandbox Code Playgroud)

sorting perl

2
推荐指数
2
解决办法
9659
查看次数

标签 统计

perl ×1

sorting ×1