Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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 ( [id://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.

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://774017]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found