in reply to Re^3: problem with sysseek in loop
in thread problem with sysseek in loop

here is what i have so far when trying to compare them.
my $dirname = "extracted"; open (my $md5file, '<', "C:/md5"); binmode($md5file); foreach my $file (<$dirname/*>) { next if -d $file; open( my $FILE, $file ); binmode($FILE); $file =~ s{.*/}{}; my $md5hash = Digest::MD5->new->addfile($FILE)->hexdigest; if ($md5hash =~ $md5file){ print "$file MATCH\n"; } else { print "$file NO MATCH!\n"; } }
I dont understand, it should /just/ work right? I dont open the file over and over and the md5 check is in the foreach loop.
Ive been at just this one part for over an hour and i am ready to give up for the evening lol.
$file =~ s{.*/}{}; just strips the path from the filename. it shouldnt have anything to do with comparing the md5's.
Any help would be appreciated :)

Replies are listed 'Best First'.
Re^5: problem with sysseek in loop
by Laurent_R (Canon) on May 08, 2014 at 09:31 UTC
    I think you should really take the habit of indenting your code correctly. This code of yours: should probably be rewritten more or less this way (no code change, just improved formatting):
    my $dirname = "extracted"; open (my $md5file, '<', "C:/md5"); binmode($md5file); foreach my $file (<$dirname/*>) { next if -d $file; open( my $FILE, $file ); binmode($FILE); $file =~ s{.*/}{}; my $md5hash = Digest::MD5->new->addfile($FILE)->hexdigest; if ($md5hash =~ $md5file){ print "$file MATCH\n"; } else { print "$file NO MATCH!\n"; } }
    as this would enable you to better visualize the logical structure and detect possible problems or bugs.

    Concerning subroutines, yes, by all means, learn them, this is quite easy and necessary. The perlsub document tells you more or less everything about them, but it has probably far more details than what you need or want for a start. For an easier introduction to them, read the relevant section (http://perldoc.perl.org/perlintro.html#Writing-subroutines) of the perlintro document, but don't read only the section on subs, reading the full document would probably be very useful to you.

      Yes i tried this and if(exists and i cannot get it to work like this for some reason, i think i would be better off to just add filename, md5hash, and any other value to a hash.
      My only question then would be, if i printed the filename and md5, would the md5 match the filename?
      ive never used hashes but was told they are great as long as your not worried about the order of the elements in them. all i can do is code it and try :)