in reply to nested loops to compare 2 files is only looping a limited number of times.

Consider what happens when something in the first file does not match anything in the second file...

Your inner loop spins through the second file until it reaches EOF. None of the other lines in the first file are going to match, because there is nothing more to match against.

One option would be to seek back to the beginning of the second file after each match, to ensure you are comparing against all the possibilities.
You might also consider loading both files into arrays so that you have less disk I/O to slog through. The downside is that it will guzzle down memory to have the entire file read in at once.
  • Comment on Re: nested loops to compare 2 files is only looping a limited number of times.

Replies are listed 'Best First'.
Re^2: nested loops to compare 2 files is only looping a limited number of times.
by stevemayes (Scribe) on Jun 23, 2009 at 13:29 UTC

    yes, the loops do break on the first failed match of file 1 against file 2.

    I can see that that is what is breaking it now that you've pointed it out but I was under the impression that when the inner loop had gone to the end of it's file it would break to the next iteration of the outer loop.

    do I need to label the out loop (e.g. LABEL:) and then do something like  if (/EOF/) { next LABEL;} then?

      When the inner loop goes to the end of it's file, it stays there. It doesn't reset the file pointer every time you enter that while loop.

      adding a seek BS, 0, 0 before your inner while loops should get your on the right track.