in reply to Re: Question of scope and syntax
in thread Question of scope and syntax

Well, that's definitely progress, and your code looks a lot better, though I recommend re-reading and understanding the latter portion of my previous post so that you can re-work your greps.

But back to your modified problem: If your code now runs, but just doesn't output what you're expecting it to, it's time to pull out the age-old debugging tool: print. Pepper your code with print statements that output the current state of various variables, which logic paths you followed, and when you're inside of loops... that sort of thing. The actual Perl debugger could be helpful, but it's harder to learn. For a short script like yours, the shortest learning curve is to just sprinkle print statements all over the place.

The idea is to look at a line of your code and think up an assertion: "I believe that before this line @something contains X, and after this line of code, @something contains that." Now put a print statement before and after demonstrating that assertion, as well as whatever variables contribute to creating the change. Run your code. Watch your assertions do what you expect, or fail. When they fail, hopefully the print statements will give you enough of a clue regarding the failure that you can figure out why it's failing.


Dave

Replies are listed 'Best First'.
Re^3: Question of scope and syntax
by Hellhound4 (Novice) on May 03, 2012 at 15:21 UTC
    I followed your advice. I have narrowed my problem down to the comparison part of my code here:
    foreach (@contentsOfDataBase){ chomp ($_); print "$_"; @inDirectoryNotInDataBase = grep {!"$_"} @contentsOfDirectory; print "@inDirectoryNotInDataBase", "\n"; } foreach (@copyOfContentsOfDirectory){ print "$_"; @pdfNoMatchingDirectory = grep (!$_, @contentsOfDataBase); print "@pdfNoMatchingDirectory", "\n"; }
    They're nearly identical so it is the same problem in both. I changed the structure of the grep function to see if that would fix it. It didn't. The problem specifically lies in the grep line itself. This is how I understand that block to work.
    foreach (@contentsOfDataBase){ chomp ($_); print "$_"; @inDirectoryNotInDataBase = grep {!"$_"} @contentsOfDirectory; print "@inDirectoryNotInDataBase", "\n"; } foreach (@copyOfContentsOfDirectory){ print "$_"; @pdfNoMatchingDirectory = grep (!$_, @contentsOfDataBase);#for curre +nt element in @copyOfContentsOfDirectory find all elements of @conten +tsOfDataBase that are not a match print "@pdfNoMatchingDirectory", "\n"; }

      The special variable, "$_" is used both by grep, and by foreach. Write your loop like this:

      foreach my $dir_try ( @copyOfContentsOfDirectory ) { push @pdfNoMatchingDirectory, grep { $_ ne $dir_try } @contentsOfD +ataBase; }

      The 'it' variable ($_) from your foreach loop is being masked by the it variable from the grep. This avoids that situation. Also, on each loop iteration, @pdfNoMatchingDirectory is being reset with a new list from grep. I added push so that @pdfNoMatchingDirectory instead accumulates all the elements you're looking for. That may not be what you're after, so you might remove the push again.

      You know, I keep seeing the word DataBase in your variables. It would probably be more efficient to use your SQL database's SQL engine to perform the set-symmetric-difference task.


      Dave

        Thank you. I actually was getting ready to post my final solution. I used a cpan module that included the exact operation I was looking for.
        #!/usr/bin/perl -w use strict; use Array::Utils qw(:all); my $boxEnd; my $pathToDirectory; my $pathToDatabase = "\\\\SHARESERVER\\DigiOfficeShare\\PerlDev\\Datab +ase.txt"; my @contentsOfDataBase; my @contentsOfDirectory; print "First Box?", "\n"; chomp (my $boxBegining = <>); print "Last Box?", "\n"; chomp ($boxEnd = <>); print "\n", "Wait a minute while I look... Lazy...", "\n"; open my $databaseOutput, $pathToDatabase || die "\n", "Cannot find fil +e.", "\n"; @contentsOfDataBase = <$databaseOutput>; chomp @contentsOfDataBase; close $databaseOutput; while ($boxEnd >= $boxBegining) { my $pathToDirectory = "\\\\SHARESERVER\\DigiOfficeShare\\PerlDev\\Bo +x $boxBegining"; $boxBegining++; opendir(my $currentDirectory, $pathToDirectory) || die "\n", "You su +re that exists, dummy?", "\n"; while(readdir $currentDirectory) { chomp $_; push(@contentsOfDirectory, $_); } closedir $currentDirectory; } @contentsOfDirectory = grep (!/^\./, @contentsOfDirectory); my @minus = array_minus( @contentsOfDirectory, @contentsOfDataBase ); my @minus2 = array_minus( @contentsOfDataBase, @contentsOfDirectory); print "\n", "These files have PDFs and no database entries:", "\n", "\ +n", "\n"; foreach (@minus){ print "\n", "$_ ", "\n"; } print "\n", "These files have databse entries and no PDFs:", "\n"; foreach (@minus2){ print "\n", "$_ ", "\n"; }