perl File::Find::Rule -重复的输出

js81xvg6  于 2023-06-23  发布在  Perl
关注(0)|答案(1)|浏览(156)

我的子例程正确地解析了我的哈希,并返回(基本上)正确的。
问题是我得到了两次回报。这是我不明白的两部分。
样本数据:

$local_h{$local}{name} = "somefile.txt";
$local_h{$local}{size} = 12345;
sub already_here {
    foreach my $local (keys(%local_h)) {
        my $tmp = $local_h{$local}{name};
        my $FFR_rule = File::Find::Rule
            ->size($local_h{$local}{size})
            ->start( @volumes );
        while ( defined ( my $match = $FFR_rule->match ) ) {
            my ( $name, $path, $suffix ) = fileparse($match);
            if ( $name =~ /$local_h{$local}{name}/ ) {
say "\t\$name $name has been matched by size and name to:\n\t $path$name\n";
#   Matches can occur multiple times, to be dealt with later/elsewhere
            } else {

say "$match  Matched by size only\n";
#   Maybe this really is the location but got renamed locally.
#   For now I will consider it an edge-case.
            }
        }
    }
}

输出:

somefile.txt has been matched by size and name to: a/path/to/somefile.txt
somefile.txt has been matched by size and name to: some/other/path/to/somefile.txt 

34thx.foo   Matched by size only

somefile.txt has been matched by size and name to: a/path/to/somefile.txt
somefile.txt has been matched by size and name to: some/other/path/to/somefile.txt 

34thx.foo   Matched by size only

我希望看到3行输出(如果算上空行,则为4行)。我完全看不出重复的根源。

bprjcwpo

bprjcwpo1#

有两种可能性。%local_h中有两个条目具有相同的名称和大小,或者您扫描同一目录两次。如果@volumes中的两个条目是它或祖先,则可以扫描同一目录两次。
也就是说,你所采取的方法是可怕的。
假设%local_h中有10个文件,磁盘上有10个文件。你将在树上行走10次。这意味着您将检查这10个文件每个文件10次,总共调用100次stat!20加20,那就是400个电话!没理由这么做在每一种情况下,以下代码分别只对stat执行20次(而不是100次)和40次(而不是400次)调用:

my %interesting;
for ( values( %local_h ) ) {
   ++$interesting{ $_->{ size } }{ $_->{ name } };
}

my $ffr = File::Find::Rule->start( @volumes );
while ( defined( my $qfn = $ffr->match ) ) {
   my $fn = basename( $qfn );
   my $size = -s $qfn;

  if ( my $r = $interesting{ $size } ) {
      if ( $r->{ $fn } ) {
         say "$fn size+name $qfn";
      } else {
         say "$fn size $qfn";
      }
   }
}

相关问题