【问题标题】:check for md5sum to identify duplicate files in perl检查 md5sum 以识别 perl 中的重复文件
【发布时间】:2015-03-19 17:29:08
【问题描述】:

如何在 if 语句中使用 perl 中的 md5sum 检查重复文件?

我正在寻找这样的代码行:

if { (md5 of new file matches any of the md5sum values of already parsed files)
print "duplicate found"
} else { new file and add md5sum to a list for check)
print "new file"
}

【问题讨论】:

标签: perl md5sum


【解决方案1】:

基本思想是为您遇到的每个文件计算一个哈希码。在伪代码中:

my %md5_to_file;

for every file
    push @{ $md5_to_file{ md5 of file } }, file

然后,%md5_to_file 映射中基数 > 1 的任何值都指向可能的重复项。然后,您可以进一步检查以确定您是否有冲突或真正的重复。

另见DFW Perl Mongers ONLINE Hackathon Smackdown - Results, Awards, And Code

【讨论】:

    【解决方案2】:

    通常执行此操作的惯用方式是使用散列。

    use strict;
    use warnings;
    use 5.018;
    
    my %seen;
    
    for my $string (qw/ one two three four one five six four seven two one /) {
        if ( $seen{$string} ) {
            say "saw $string";
        }
        else {
            $seen{$string}++;
            say "new $string";
        }
    }
    

    How is the hash used to find unique items 更详细。

    正如评论中提到的,您将使用像 Digest::MD5 这样的库来为文件生成 MD5 字符串。将两者联系在一起留给读者一个练习。

    【讨论】:

      猜你喜欢
      • 2012-12-11
      • 1970-01-01
      • 1970-01-01
      • 2010-11-11
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      • 1970-01-01
      • 2017-09-11
      相关资源
      最近更新 更多