I re-coded with the idea that the loop is over when an EOF has been seen at least once on both files. I don't have any big issue with the use of the comma statement, but since this could be confusing, I expanded that part of the code into 2 discrete statements.
"eof" is also nice because you get that status right after reading the last valid line. The next read on that file handle would produce an undefined line which often ends many while() input loops, e.g. while ($line = <IN>){}. By using "eof" instead of an undefined read, no "re-reading" is necessary++, very nice.#!usr/bin/perl use strict; use warnings; open ONE, '<' ,'one.txt' or die "Ooops $!"; open TWO, '<' ,'two.txt' or die "Ooops $!"; my $EOFseenfile1=0; my $EOFseenfile2=0; # Loop finished when EOF has been seen at least once # on both files. while (not $EOFseenfile1 or not $EOFseenfile2) #go until both EOF seen { my $line1 = <ONE>; my $line2 = <TWO>; chomp ($line1, $line2); print "$line1 \t $line2\n"; if (eof ONE) { seek ONE,0,0; $EOFseenfile1++; } if (eof TWO) { seek TWO,0,0; $EOFseenfile2++; } } __END__ file 1 line 1 file 2 line 1 file 1 line 2 file 2 line 2 file 1 line 3 file 2 line 3 file 1 line 1 file 2 line 4 file 1 line 2 file 2 line 5 file 1 line 3 file 2 line 6 file 1 line 1 file 2 line 7 file 1 line 2 file 2 line 8 file 1 has 3 lines file 2 has 8 lines
Update:
Geez, if guess while (not $EOFseenfile1 or not $EOFseenfile2){} could be while (!($EOFseenfile1 and $EOFseenfile2)){}. I don't know why I coded the first version. These two while conditionals are equivalent.
In reply to Re^2: writing two files (different in length) to one output
by Marshall
in thread writing two files (different in length) to one output
by ic23oluk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |