Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: nested loops to compare 2 files is only looping a limited number of times.

by si_lence (Deacon)
on Jun 23, 2009 at 13:37 UTC ( #774017=note: print w/ replies, xml ) Need Help??


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

As noted above by SuicideJunkie you don't start from the beginning of the second file for each pass trough the outer loop, i.e. for each line of file1.

If your second file is not too big (compared to memory available) I would suggest to read the second file into a hash before the first loop and then loop through file1 and look for matches. You only need one loop then and not the two nested ones.

cheers

si_lence


Comment on Re: nested loops to compare 2 files is only looping a limited number of times.
Re^2: nested loops to compare 2 files is only looping a limited number of times.
by ack (Deacon) on Jun 23, 2009 at 15:39 UTC

    I especially like si_lence's hash idea. That is what I usually do for exactly the reason that si_lence notes: since the files are not that long, the hash approach allows you to (1) dispense with one of the loops in the nest and (2) makes for very quick locating of matching entries.

    I am embarassed that I didn't see that when I posted my response earlier to the excellent stevemayes response.

    ack Albuquerque, NM

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://774017]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-08-22 03:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (146 votes), past polls