Provided that the files are not overly large, the following algorithm should suffice -
Step 1 - open the first file, while reading each line, extract the $login from the line, and then add the entry
$login => $rest_of_the_line in a hash table.
Step 2 - open the second file, while reading each line, extract the $login from the line, and then look up the $login in the hash table built from the first file.
Step 3 - if a match is found, then do compare/combine/whatever with the data stored in the hash table and the data held in current line. Output the result to the third file.
Ok, sounds like you need some code...
use strict;
use IO::File;
my %data; # hash to store data from first file
my $f = new IO::File "first.txt", "r" or die "Can't open file 1";
while (<$f>) {
chomp;
my ($login) = /^(\w+):/;
$data{$login} = $_;
}
undef $f;
my $f = new IO::File "second.txt", "r" or die "Can't open file 2";
my $o = new IO::File "third.txt", "w" or die "Can't create file 3";
while (<$f>) {
chomp;
my ($login,$info) = /^(\w+)(:.*)/;
if ($data{$login}) {
print $o $data{$login} . $info, "\n";
}
}
undef $f;
undef $o;
And the files -
---- first.txt ----
0001:rec1:rec2:rec3
0002:recA:recB:recC
0003:recX:recY:recZ
---- second.txt ----
0001:rec4:rec5:rec6
0002:recD:recE:recF
The output file -
---- third.txt ----
0001:rec1:rec2:rec3:rec4:rec5:rec6
0002:recA:recB:recC:recD:recE:recF
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.