我有以下代码,我想查看目录中的最后3个文件夹
#read folders
opendir (DIR, "$path1") or die $!;
while ($file = readdir(DIR)) {
next if ($file eq "." or $file eq "..");
next unless (-d "$path1/$file");
if ($file =~ "@01-"){
#print "$file\n";
push @nr, $file;
for ($i = 0; $i < scalar(@nr)-7; $i++) {
print "@nr[$i]\n"; #here is the problem
}
}
}
closedir(DIR);
exit 0;
答案 0 :(得分:1)
您的代码和您的问题都很糟糕,但我认为您尝试做的是
use strict;
use warnings;
use File::Spec::Functions qw/ catfile /;
use List::Util qw/ max /;
my $path1 = '~/data';
my $prefix = 'V120';
my $tail = 3;
my @nr;
opendir my ($dh), $path1 or die $!;
while ( my $file = readdir $dh ) {
next if $file eq '.' or $file eq '..';
next unless $file =~ /^$prefix-/;
next unless -d catfile($path1, $file);
push @nr, $file;
}
for my $i ( max(@nr - $tail, 0) .. $#nr ) {
print $nr[$i], "\n";
}