Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: compare md5 from file against entire@md5 array

by sundialsvc4 (Abbot)
on Dec 04, 2013 at 14:00 UTC ( #1065591=note: print w/replies, xml ) Need Help??

in reply to compare md5 from file against entire@md5 array

A foreach loop is also handy, e.g.:

my $found = 0; foreach my $element (@array) { if ($element eq $md5) { $found = 1; last; # STOP THE LOOP EARLY } }

This is a simple, obvious strategy that should be perfectly suitable for up to a few hundred thousand MD5s on today’s hardware.

Replies are listed 'Best First'.
Re^2: compare md5 from file against entire@md5 array
by james289o9 (Novice) on Dec 04, 2013 at 14:16 UTC
    you guys are the BOMB xD ill have to check that snippet out, this will stop searching the array if it finds a match it right? the first link helped me alot, now im comparing hashes from argv0 against my array :D
      just to update, this is how i accomplished this.
      use warnings; use strict; use Digest::MD5; my $file = "$ARGV[0]"; open (my $fh, '<', $file) or die "Can't open '$file': $!"; binmode ($fh); my $md5 = Digest::MD5->new->addfile($fh)->hexdigest; print "\n", $file, "\n", $md5, "\n"; my @array = ('d41d8cd98f00b204e9800998ecf8427e', 'd6721344ed0cdc2e8a05 +4a68b7ebc365', 'cee8eb94fd83f8d534bc44bf136ebaa0'); if( $md5 ~~ @array ) { print "The MD5's match perfectly\n"; } else { print "The MD5's do NOT match\n"; } system ( 'pause' );
      thanks to you all for helping me with this :)

        Why not just use a hash as a simple lookup table?

        This refactored version of your script prints a tab-separated list of all the files specified as arguments on the command line. The report includes a Boolean column indicating if each file's MD5 digest matches one of the digests in the lookup table:  Yes or No.

        #!perl use strict; use warnings; use autodie qw( open close ); use Digest::MD5; use English qw( -no_match_vars ); use File::Glob qw( bsd_glob ); @ARGV or die "Usage: perl $PROGRAM_NAME file ...\n"; @ARGV = map { bsd_glob($ARG) } @ARGV; local $OUTPUT_FIELD_SEPARATOR = "\t"; local $OUTPUT_RECORD_SEPARATOR = "\n"; my %md5_digests_table = map { $ARG => 1 } qw( d41d8cd98f00b204e9800998ecf8427e d6721344ed0cdc2e8a054a68b7ebc365 cee8eb94fd83f8d534bc44bf136ebaa0 ); for my $file (@ARGV) { open my $fh, '<', $file; binmode($fh); my $md5 = Digest::MD5->new()->addfile($fh)->hexdigest(); close $fh; my $match = exists $md5_digests_table{$md5} ? 'Yes' : 'No'; print $file, $md5, $match; } exit 0;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1065591]
[choroba]: Good morning!
LanX frowning!
[Corion]: Hi choroba, LanX!
[Corion]: LanX: Turn that frown upside down! ;)

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2018-05-23 07:35 GMT
Find Nodes?
    Voting Booth?