A somewhat generalized solution to the problem allows a arbitrary number of files to be handled:
use strict;
use warnings;
my $file1 = <<'END_FILE';
1 G
2 F
4 H
8 T
END_FILE
my $file2 = <<'END_FILE';
5 Y
6 R
7 E
8 I
END_FILE
my $file3 = <<'END_FILE';
2 H
4 T
3 G
8 R
END_FILE
my @fileHandles;
open $fileHandles[@fileHandles], '<', $_ for \$file1, \$file2, \$file3
+;
printLines (@fileHandles);
close $_ for @fileHandles;
sub printLines {
my @fileHandles = @_;
my $moreLines = 1;
while ($moreLines) {
my $line = '';
for my $fh (@fileHandles) {
$line .= ' ' if length $line;
$line .= <$fh> or return;
chomp $line;
}
print "$line\n";
$line = '';
}
}
Prints:
1 G 5 Y 2 H
2 F 6 R 4 T
4 H 7 E 3 G
8 T 8 I 8 R
True laziness is hard work
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.