I'd like to have a perl script create a tab delimited txt out of two text files, line by line.

I.e. I have:
File 1: one two three four File 2: A B C D
And I'd like the script to generate:
one[tab]A two[tab]B three[tab]C four[tab]D

Ideally, this would be done without reading any of the files into memory in full, as I'd like it to work on large files on systems with little memory. (Although I'd like to know how it's best done by reading the files into variables, God knows I need some guidance on arrays and the like.)

I feel like some sort of a while loop should work, but it would need to iterate through two files at once, which sounds tricky... Maybe I could create a while loop that goes through file 1, saves the current line no. and the current line content in variables, and embed another while loop in it that goes through file 2 and prints the output when it gets to the line number that loop 1 is at. That should work, but that way I'd loop through file 2 lots of times.

Any ideas for doing this in a more elegant/efficient way?

Update: solved, my favourite solution for files of equal length is (from almut):
while (my $col1 = <FILE1>) { chomp $col1; my $col2 = <FILE2>; print "$col1\t$col2"; }
And, if the two files may have a different number of lines, courtesy of BrowserUk and Marshall:
until(eof(ONE) and eof (TWO)) { my $one = <ONE>; my $two = <TWO>; $one ||= ""; $two ||= ""; chomp($one); chomp($two); print "$one\t$two\n"; }

In reply to How do you create a tab delimited txt file from two files? by elef

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.