Well, this should do the trick. I did not do any bounds
error checking on the arrays that hold the files. This
will just give you an idea how to solve the problem:
use strict;
my (@lines1, @lines2, @lines3);
&load_array(\@lines1, "foo1.dat");
&load_array(\@lines2, "foo2.dat");
&load_array(\@lines3, "foo3.dat");
for (my $i = 0; $i < @lines1; $i++) {
my ($word2) = split(/ /, $lines2[$i]);
my ($word3) = split(/ /, $lines3[$i]);
print "$lines1[$i]:$word2:$word3\n";
}
sub load_array($$) {
my ($a_ref, $file) = @_;
open (FILE, $file) or die "Can't open $file:$!\n";
@{$a_ref} = <FILE>;
close FILE;
chomp @{$a_ref};
}
As you can see, this will bomb if the first file contains more lines
than the other two files - recursive turnary operator, anyone?
There is lots of improving that can be done to this
rather unelegant piece of code . . . but this does
the trick - hope this helps.
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.