我使用下面的代码从目录中读取文件名,并推动他们到一个数组:
#!/usr/bin/perl
use strict;
use warnings;
my $directory="/var/www/out-original";
my $filterstring=".csv";
my @files;
# Open the folder
opendir(DIR, $directory) or die "couldn't open $directory: $!\n";
foreach my $filename (readdir(DIR)) {
if ($filename =~ m/$filterstring/) {
# print $filename;
# print "\n";
push (@files, $filename);
}
}
closedir DIR;
foreach my $file (@files) {
print $file . "\n";
}
我从这段代码运行得到的输出是:
Report_10_2014.csv
Report_04_2014.csv
Report_07_2014.csv
Report_05_2014.csv
Report_02_2014.csv
Report_06_2014.csv
Report_03_2014.csv
Report_01_2014.csv
Report_08_2014.csv
Report.csv
Report_09_2014.csv
这是为什么代码推送的文件名到阵列的顺序,而不是从01
至10
?