Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Loop through 2 files in parallel

by AndyZaft (Hermit)
on Nov 10, 2010 at 20:04 UTC ( [id://870671] : note . print w/replies, xml ) Need Help??


in reply to Loop through 2 files in parallel

while ($line_f1 = <FILE>) { while($line_f2 = <FILE2>) { # I'm sure the rest you can do it } seek(FILE2,0,0); }
could be one solution. If it was me however, I would probably read the smaller file into an array or another structure and just go through the other one, instead of reading lines from the second loop as many times as the amount of lines the first one has.

Replies are listed 'Best First'.
Re^2: Loop through 2 files in parallel
by sherab (Scribe) on Nov 10, 2010 at 20:11 UTC
    The Perl Cookbook might be helpful here "Finding elements in one array but not another".I'm with AndyZaft, putting each of these into an array and then doing your logic is the way to go.
      This is a very good idea (the hash keys) but it won't work in my situation because the elements of the list are not unique. They can occur multiple times in the list, so how far along in the list you are makes a difference. I think you're right about the array. If I put each file into an array I can keep track of where I left off and move around. I was just trying to figure out how to do it without using an array because the lists are pretty long, 20K lines each. I guess that isn't much for Perl, but it would be better for keeping what's loaded into memory down if it wasn't all put into an array at once.