Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Perl-Sensitive Sunglasses
 
PerlMonks  

compare md5 from file against entire@md5 array

by james289o9 (Novice)
on Dec 04, 2013 at 13:14 UTC ( #1065586=perlquestion: print w/ replies, xml ) Need Help??
james289o9 has asked for the wisdom of the Perl Monks concerning the following question:

i wrote this script and finally figured out, after hours and hours of searching, how to compare two md5 checksums:
use warnings; use strict; use Digest::MD5; my $file = "$ARGV[0]"; my $file1 = "$ARGV[1]"; open (my $fh, '<', $file) or die "Can't open '$file': $!"; binmode ($fh); open (my $fh1, '<', $file1) or die "Can't open '$file': $!"; binmode ($fh1); my $md5 = Digest::MD5->new->addfile($fh)->hexdigest; my $md51 = Digest::MD5->new->addfile($fh1)->hexdigest; print "\n", $file, "\n", $md5, "\n", "\n", $file1, "\n", $md51, "\n"; if ($md5 eq $md51) { print "\n", "The MD5's match perfectly", "\n"; } else { print "\n", "The MD5's do NOT match", "\n"; } system ( 'pause' );
but what i really need to do is get the md5 from $ARGV0 and compare that against an entire predefined @md5 array. essentially the $ARGV1 will be replaced by an array. i have searched and searched how to accomplish this. Like i say, all i need to do is compare the ARGV0 md5 against an entire array of md5's. any help or input would be very much appreciated :)

Comment on compare md5 from file against entire@md5 array
Download Code
Re: compare md5 from file against entire@md5 array
by hippo (Chaplain) on Dec 04, 2013 at 13:21 UTC
      you sir are a life saver. i kept googling for "how to compare md5 against an entire array". and there wasnt much info :( heck, i couldnt even find that FAQ sheet you linked lol. thanks sir/ma'am

      I am curious why, if smartmatch is experimental, perlfaq4 references only with such high prominence the smartmatch solution (thanks to choroba for the link).

      --MidLifeXis

Re: compare md5 from file against entire@md5 array
by MidLifeXis (Prior) on Dec 04, 2013 at 13:42 UTC

    Depending on the age of your perl, you may also want to look at grep.

    --MidLifeXis

      im using active state perl. the latest build 64 bit. but i will still look into grep. thanks :)
Re: compare md5 from file against entire@md5 array
by sundialsvc4 (Monsignor) on Dec 04, 2013 at 14:00 UTC

    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.

      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 :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1065586]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-04-18 10:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (465 votes), past polls