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

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

Replies are listed 'Best First'.
Re^5: Question of scope and syntax
by Hellhound4 (Novice) on May 03, 2012 at 17:47 UTC
    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"; }