http://www.perlmonks.org?node_id=477415

omega_monk has asked for the wisdom of the Perl Monks concerning the following question:

Monks,
I am working on comparing (2) hashes of filenames/md5sum combinations, and I am stuck...What I am doing is using File::Find to get a recursive list of the files from the given(command line parm) dirs, both source, and destination. then I want to md5 all the files that I just made the list of. That is working fine, and here is what I have right now, with the error in the foreach loop.
my %sdirdigest{ "c:/temp/filename.txt"=>"4eb788842f1253903e16a1a0cfe +46f2f", "c:/temp/level2/filename2.txt"=>"fb3e103dee9f40881fb +e8fdccdc4d07a" }; my %ddirdigest{ "e:/temp/filename.txt"=>"4eb7adfaaf1253903e16a1a0cfe +46f2f", "e:/temp/level1/filename2.txt"=>"1fbe8fdccdc4d07afb3 +e103dee9f4088" }; foreach my $key (keys %sdirdigest) { print "Checking $key..." if $verbose == 1; if ((defined($ddirdigest{$key})) && ($sdirdigest{$key} eq $ddirdige +st{$key})) { print "SYNCHRONIZED...\n" if $verbose == 1; } else { print "UPDATING...\n" if $verbose == 1; my $todo = 'cp "'.$key.'" "'.$key.'"'; system($todo); } }

I know that the issue here is that I am using $key wrong, which is from when I was not doing a recursive check, if that makes sense. I am pretty sure that my issue is from design problems, but I am not sure what I need to do. I tried searching, but came up empty handed, due to the fact that I am not really sure what I need to do here. I don't need the answer specifically, just a pointer in the right direction. If I need more information, please let me know what will help.
Thanks.

update: Updated with the rest of my code.
#!/usr/bin/perl use warnings; use strict; use diagnostics; use Cwd; use File::Find; use Digest::MD5; use File::Basename; use Getopt::Long; my %fdirdigest; my %sdirdigest; my $fdircount='0'; my $sdircount='0'; my($fbn,$fdn,$fdg,$bn,$dn,$dg,@fdir_list,@sdir_list); my $fdir=''; my $sdir=''; my $recursive='0'; my $verbose='0'; GetOptions( 'verbose'=>\$verbose, 'recursive'=>\$recursive, 'src=s'=>\$fdir, 'dst=s'=>\$sdir ) or die "Oops, check your command line pa +rms. $^E"; if (!(-e $fdir && -e $sdir)) { print 'I can not proceed, since both dirs do not exist.'."\n"; exit(); } if ($recursive == 1) { find(\&push_to_fdir,$fdir); find(\&push_to_sdir,$sdir); } else { @fdir_list = glob("$fdir*"); @sdir_list = glob("$sdir*"); } foreach my $filef (@fdir_list) { if (-d $filef) { next(); } my $fdg; $fbn=basename($filef); $fdn=dirname($filef); chdir($fdn); my $fmd5 = Digest::MD5->reset; open(FILE,$fbn) or die "Unable to open the file: $^E\n"; binmode(FILE); while (<FILE>) { $fmd5->add($_); } close(FILE); $fdg = $fmd5->hexdigest(); $fdirdigest{"$filef"}=$fdg; $fdircount++; } foreach my $files (@sdir_list) { if (-d $files) { next(); } my $dg; $bn=basename($files); $dn=dirname($files); chdir($dn); my $md5 = Digest::MD5->reset; open(FILE,$bn) or die "Unable to open the file: $^E\n"; binmode(FILE); while (<FILE>) { $md5->add($_); } close(FILE); $dg = $md5->hexdigest(); $sdirdigest{"$files"}=$dg; $sdircount++; } foreach my $key (keys %fdirdigest) { print "Checking $key..." if $verbose == 1; if ((defined($sdirdigest{$key})) && ($fdirdigest{$key} eq $sdirdige +st{$key})) { print "SYNCHRONIZED...\n" if $verbose == 1; } else { print "UPDATING...\n" if $verbose == 1; my $todo = 'cp "'.$key.'" "'.$key.'"'; system($todo); } } print "\nFiles Processed:\t$fdir\t$fdircount\n"; print " \t$sdir\t$sdircount\n"; sub push_to_fdir { my $dir = getcwd; my $fp = "$dir/$_"; #print "$fp\n"; push(@fdir_list, "$fp") unless $_ =~ /^\.$|^\.\.$|[T|t]humbs.db/ or + $fp =~ /\/\//; } sub push_to_sdir { my $dir = getcwd; my $fp = "$dir/$_"; #print "$fp\n"; push(@sdir_list, "$fp") unless $_ =~ /^\.$|^\.\.$|[T|t]humbs.db/ or + $fp =~ /\/\//; }