从所需位置打印字符串perl



我有以下代码,我想看到目录

的最后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 "$filen";
        push @nr, $file;
        for ($i = 0; $i < scalar(@nr)-7; $i++) {
             print "@nr[$i]n"; #here is the problem
        }
    }
} 
closedir(DIR);
exit 0;

你的代码和问题都很糟糕,但我认为你想做的是

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";
}

最新更新