我在perl中有一个子模块来比较有关其时间的文件。 现在我不仅要比较目录A中的“lin”和目录B中的“LINT”。如何在A和B中添加更多文件?
...
sub find_newer_files {
my ( $dirA, $extA, $dirB, $extB ) = @_;
.....
find_newer_files(
'D:/projects/Check_Tool/AGF/lux/2016_12' => "lin", # directory_A => extension_A
'D:/projects/Check_Tool/TAGF/lux' => "LINT", # directory B
);
...
是的,这是我需要的。因此,我可以像在[ "lin1", "lin2" ... ]
中一样在数组中添加更多扩展名。
我的子程序的其余部分是:
sub find_newer_files {
my ( $dirA, $extA, $dirB, $extB ) = @_;
# read files and their mtime from dirA into a hash (fname => mtime)
chdir $dirA or die "chdir($dirA) failed: $!";
my %files_in_A = map { $_ => ( stat $_ )[9] } glob( "*.$extA" );
# read files and their mtime from dirB into a hash (fname => mtime)
chdir $dirB or die "chdir($dirB) failed: $!";
my %files_in_B = map { $_ => ( stat $_ )[9] } glob( "*.$extB" );
# for each found file in dirA
for my $fileA ( keys %files_in_A ) {
# replace extension for wanted file in dirB
( my $fileB = $fileA ) =~ s{\.\Q$extA\E$}{.$extB};
warn "$fileA -> $fileB\n" if $ENV{DEBUG};
# if we found a fileB in dirB
if ( exists $files_in_B{$fileB} ) {
# compare the timestamp; tell us when fileB is older than fileA
if ( $files_in_A{$fileA} > $files_in_B{$fileB} ) {
say "$fileB is older than $fileA"; #say
}
}
}
答案 0 :(得分:-1)
我猜你的意思是:
...
sub find_newer_files {
my ( $dirA, $extA, $dirB, $extB ) = @_;
my @found;
if (ref $extA) {
foreach my $i(0 .. $#{$extA}) {
push @found,
find_newer_files($dirA, $extA->[$i], $dirB, $extB->[$i]);
}
return @found;
}
.....
find_newer_files(
'D:/projects/Check_Tool/AGF/lux/2016_12' => "lin", # directory_A => extension_A
'D:/projects/Check_Tool/TAGF/lux' => "LINT", # directory B
);
find_newer_files(
'D:/projects/Check_Tool/AGF/lux/2016_12' => ["lin1","lin2","lin3"], # directory_A => extension_A
'D:/projects/Check_Tool/TAGF/lux' => ["LINT1","LINT2","LINT3"], # directory B
);
...
添加此块
my @found;
if (ref $extA) {
foreach my $i(0 .. $#{$extA}) {
push @found,
find_newer_files($dirA, $extA->[$i], $dirB, $extB->[$i]);
}
return @found;
}
到您的函数顶部,您可以检查其他扩展(假设您不返回任何内容或文件列表)。
所以不要写:
find_newer_files(
'D:/projects/Check_Tool/AGF/lux/2016_12' => "lin1", # directory_A => extension_A
'D:/projects/Check_Tool/TAGF/lux' => "LINT1", # directory B
);
find_newer_files(
'D:/projects/Check_Tool/AGF/lux/2016_12' => "lin2", # directory_A => extension_A
'D:/projects/Check_Tool/TAGF/lux' => "LINT2", # directory B
);
find_newer_files(
'D:/projects/Check_Tool/AGF/lux/2016_12' => "lin3", # directory_A => extension_A
'D:/projects/Check_Tool/TAGF/lux' => "LINT3", # directory B
);
你可以使用
find_newer_files(
'D:/projects/Check_Tool/AGF/lux/2016_12' => ["lin1","lin2","lin3"], # directory_A => extension_A
'D:/projects/Check_Tool/TAGF/lux' => ["LINT1","LINT2","LINT3"], # directory B
);