Here's another way to do it using Tie::File and List::MoreUtils::pairwise().

Here's the input data:

$ cat pm_1075362_1.txt file 1 line 1 file 1 line 2 $ cat pm_1075362_2.txt file 2 line 1 file 2 line 2 file 2 line 3 $ cat pm_1075362_3.txt file 3 line 1 file 3 line 2

Here's the script showing examples for: a) first file shorter than the second; b) first file longer than the second; c) both files with same length.

#!/usr/bin/env perl -l use strict; use warnings; use autodie; use Tie::File; use List::MoreUtils qw{pairwise}; my ($file1, $file2, $file3) = map { "pm_1075362_$_.txt" } 1 .. 3; tie my @file1, 'Tie::File', $file1; tie my @file2, 'Tie::File', $file2; tie my @file3, 'Tie::File', $file3; print '*** File 1 (2 lines) and File 2 (3 lines):'; print_tabbed_pairs(\@file1, \@file2); print '*** File 2 (3 lines) and File 1 (2 lines):'; print_tabbed_pairs(\@file2, \@file1); print '*** File 1 (2 lines) and File 3 (2 lines):'; print_tabbed_pairs(\@file1, \@file3); sub print_tabbed_pairs { my ($file1, $file2) = @_; print for pairwise { join "\t" => (defined $a ? $a : ''), (defined $b ? $b : '') } @$file1, @$file2; }

Output:

*** File 1 (2 lines) and File 2 (3 lines): file 1 line 1 file 2 line 1 file 1 line 2 file 2 line 2 file 2 line 3 *** File 2 (3 lines) and File 1 (2 lines): file 2 line 1 file 1 line 1 file 2 line 2 file 1 line 2 file 2 line 3 *** File 1 (2 lines) and File 3 (2 lines): file 1 line 1 file 3 line 1 file 1 line 2 file 3 line 2

-- Ken


In reply to Re: Use 2 files and print each line of each one side-by-side by kcott
in thread Use 2 files and print each line of each one side-by-side by Anonymous Monk

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.