I would make an ArrayOfArray to keep the results. I would make a loop to open each file, read it line by line, use column 1 as the index to the ArrayOfArray and push col3 onto the appropriate array specified by the array index and then loop to the next file. Once you are done, the ArrayOfArray can be printed to a new file.
What code have you written so far? What are the problems? A loop is the appropriate answer for repetitive operations like this. The data will fit into memory at once and only one file at a time needs to be open.
Update: I think something like this would work:
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my $datafile1=<<END;
1 has 0.334
2 has 0.986
3 has 0.787
END
my $datafile2=<<END;
1 has 0.894
2 has 0.586
3 has 0.187
END
my @data;
foreach my $fileRef (\$datafile1, \$datafile2)
{
open FILE, '<', $fileRef or die "$!";
while (<FILE>)
{
my ($row, $col3) = (split)[0,2];
push @{$data[--$row]}, $col3;
}
}
my $row_num=1;
foreach my $row (@data)
{
print $row_num++, " has ", "@$row\n";
}
__END__
1 has 0.334 0.894
2 has 0.986 0.586
3 has 0.787 0.187
Of course instead of using references to files, you would need to use some form of glob() or readdir() to get the file names. And of course the data that was presented is not a CSV file so, something would have to be done about that.
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.