Forum Moderators: coopster & phranque

Message Too Old, No Replies

looping inside perl loops

getting strings from two files

         

RudyS

4:34 pm on Apr 7, 2008 (gmt 0)

10+ Year Member



im getting loopy trying to figure this out

I have two output files with various numbers of lines ... every line contains one number and, to simplify, only one number ... I want the script to take the first line of file1 and compare it with every line in file2, then move to the second line of file1 and compare it to every line of file2 ... etc

depending how i write the program i get either the first line of file1 compared to every line of file2, or each line of file1 compared to each line of file2

the script below prints to OUT only the first line of INLEFT with each line of INRIGHT ... it prints to the console the first line of INLEFT with each line of INRIGHT followed by only the first line of INLEFT ... this must be a very amateurish problem, but i would appreciate very much your help

Rudy

#!/usr/bin/perl -w
open (OUT, '>printout.txt');

open (INLEFT, '<outlefts');
open (INRIGHT, '<outrights');

while (<INLEFT>)
{
print $_;
$posleft = $_;

foreach (<INRIGHT>)
{
print $_;
$posright = $_;

print "$posleft\t$posright\n";
if ($posleft < $posright)
{
print OUT "$posleft\t$posright\n";
}
}
}

[edited by: phranque at 10:10 pm (utc) on April 7, 2008]
[edit reason] disable smileys [/edit]

adwatson

5:24 pm on Apr 7, 2008 (gmt 0)

10+ Year Member



Without testing, my bet would be that your second loop (the foreach one) is looping through the 2nd file then ending because its at the end of the file - and it's not being reset to the top of the file again. I don't recall the way to this, but I'm pretty sure it's possible.

The other option would be to read the 2nd file into an array of lines before you start looping through the first file - then just loop through the array instead of messing with the file handle for the 2nd file. Might be faster too.

perl_diver

5:30 pm on Apr 7, 2008 (gmt 0)

10+ Year Member



That is correct. Also "foreach" is the wrong control for looping through a file because it does not check for EOF like a "while" loop will. You can reset to the beginning of a file using seek().

phranque

12:02 am on Apr 8, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], RudyS!

i think the only thing you need to do to fix that script is to add the following between the last two closing braces as suggested by p_d:
seek(INRIGHT,0,0);

RudyS

3:04 pm on Apr 8, 2008 (gmt 0)

10+ Year Member



phranque,perl_diver,adwatson

thanks for the clarification of p_d's suggestion to add a seek ... seek(INRIGHT,0,0) works nicely placed before the final rightbrace ... very much appreciation also for adwatson's suggestion on the array of lines ... if i need efficiency i will try that way ...

and thanks for the warm welcome!

perl_diver

6:51 pm on Apr 8, 2008 (gmt 0)

10+ Year Member



You're welcome. I often leave out details of how to use a perl builtin function because anyone can look those up in the perl documentation. In this case the seek() function documentation maybe would not have helped unless you already are familiar with how fseek works. Basically 0,0 means reset the filehandle position to the 0 bytes position with 0 bytes offset, so essentially you tell seek() to return to the beginning of the file.

RudyS

3:48 am on Apr 9, 2008 (gmt 0)

10+ Year Member



p_d
i read the documentation on seek(), but what i got wrong that phranque straightened me out on was the choice of which filehandle to reference ... i.e., INRIGHT instead of what i was thinking was correct after reading your advice ... (INLEFT) ... i learned a lot trying to get it to work before phranque popped in so it wasnt wasted effort ... btw: i originally found this site from a google search that listed a thread that you contributed to and then i read some other of your comments and got the encouragement to post a help request ... so double thanks to you p_d
rudyS

phranque

4:43 am on Apr 9, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



yeah we like having perl_diver around here - even if he doesn't get worked hard enough.
=8)

perl_diver

7:01 am on Apr 9, 2008 (gmt 0)

10+ Year Member



Thanks for the kind words folks.