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]
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.
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);
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!