I really liked your solution! However, I think there is a small problem with the loop conditional. $go gets decremented every time a seek to the beginning of a file is done. This worked fine in your test case, but what if file1 needs to gets "rewound" many times? $go could get decremented a bunch of times before an eof on TWO happens.

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.

#!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
"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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.