Reading Directory Mystery

Discussion in 'Perl Misc' started by ChristopherSimone@gmail.com, Feb 26, 2007.

  1. Guest

    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?
     
    , Feb 26, 2007
    #1
    1. Advertising

  2. Guest

    Figured it out. I was using open $OD instead of opendir $OD.

    On Feb 26, 12:55 pm, wrote:
    > 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?
     
    , Feb 26, 2007
    #2
    1. Advertising

  3. Joe Smith Guest

    wrote:

    > 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
     
    Joe Smith, Feb 26, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. valentin tihomirov

    another array ranges mystery

    valentin tihomirov, Jun 18, 2005, in forum: VHDL
    Replies:
    2
    Views:
    491
    Mike Treseler
    Jun 18, 2005
  2. John K. Humkey

    sed regexp mystery

    John K. Humkey, Jul 8, 2003, in forum: Perl
    Replies:
    0
    Views:
    2,061
    John K. Humkey
    Jul 8, 2003
  3. bjh

    Mystery Repost

    bjh, Jan 13, 2004, in forum: Perl
    Replies:
    0
    Views:
    742
  4. Todd Plambeck

    Mystery Error

    Todd Plambeck, Jul 15, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    519
    Victor Garcia Aprea [MVP]
    Jul 15, 2003
  5. Jim Hammond

    datagrid template column binding mystery

    Jim Hammond, Nov 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    554
    Jim Hammond
    Nov 24, 2003
Loading...

Share This Page