Reading Directory Mystery

C

ChristopherSimone

I am working on a hashing program that reads a .ini file and creates a
md5hash of any directory listed within. I am checking properly if the
file still exists and checks the hash and if a new file has been
created and there is no saved hash. What I am trying to fix now is if
I have a stored hash of a file and that file is no longer in the place
it should. Listed below is my code and what I am seeing.

sub missing
{
print "ok1\n";
my $newOD;
open $newOD, $digestdir or die "Cannot open digestdir: $!\n";
#digestdir is where the md5 digest files are

#stored
print "ok2\n";

while( defined( my $x = readdir $newOD ) )
{
print "inside";
my $count = 0;

if( -f "$digestdir/$x" )
{
print "$x\n";
# %hashed is a hash of all my md5s. It is populated with file/hash
pairs from the original directory.
# If I have a file in my digest directory that matches up with one of
the pairs then the program will not worry about
# it. If it doesn't find a match that means a file that had a hash no
longer exists.
foreach my $j (keys %hashed)
{
if( $j eq "$digestdir/$x" )
{
$count++;
}
}
}

if( $count == 0 )
{
print "This file no longer exists: $x\n";
}
}
print "outside\n";
close $newOD;
}

When I run the program I get the following output:
ok1
ok2
outside

The program seems to skip the while loop all together (I am guessing
that my condition is not defined), but I use the exact same code in a
similar manner in another section that seems to work fine. Maybe it
has something to do with the scope of the directory handle, but I made
sure that it was local to the sub and not used elsewhere in the
program. Any advice?
 
J

Joe Smith

foreach my $j (keys %hashed)
{
if( $j eq "$digestdir/$x" )
{
$count++;
}
}

That's dumb. There is no reason to loop over all the keys in
a hash just to check if a given key is being used.
Just access the hash like it is supposed to be accessed.

$count++ if exists %hashed{"$digestdir/$x"};

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top