Always, and without fail, include the following at the top of your Perl scripts:

use strict; use warnings;

These pragmas will likely save you many headaches by showing you problematic areas in your script.

Use the three-argument form of open. For example:

open $USER1, '<', $userfile1 or die "Couldn't open file: $userfile1 - +$!"; ... while(<$USER1>) { ...

Note also the use of $!. It's good that you're handling errors; it's better if you're shown exactly what produced an exception if there's an error, and $! will let you know.

You need to do the same when opening a file for writing. However, you can do the just following when opening files:

open $OUT, '>', "CONCATENATED_FILES"; ... open $USER1, '<', $userfile1;

if you include the following pragma:

use autodie;

Looping through both files is a good way to achieve your desired results, and roboticus provided a solution for handling reading from both files. Since, however, your running this script from the command line, here's another option--in case you may be interested:

use strict; use warnings; use File::Slurp qw/read_file/; use List::MoreUtils qw/zip/; chomp( my @file1 = read_file $ARGV[0] ); chomp( my @file2 = read_file $ARGV[1] ); my @combined = zip @file1, @file2; for my $i ( 0 .. $#file1 ) { last if !defined $file1[$i] or !defined $file2[$i]; print "$file1[$i]\t$file2[$i]\n"; }

Usage: perl script.pl file1 file2 >combinedFile

The script uses File::Slurp to read each file's contents into an array. Next, is uses List::MoreUtils to 'zip' or interleave the elements of the two arrays. When iterating through the arrays for printing, the script checks that both elements are defined in case one file has more lines than the other.

The above script will work just fine with small or not-too-large files, since their entire contents are read into arrays. If, however, your files are large, stick with just iterating through each file, a line at a time.

Hope this helps!


In reply to Re: Use 2 files and print each line of each one side-by-side by Kenosis
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.