First, look at the perlmonks
Site How To page for instructions on using the <code> tag when posting your code -- it will retain the indentation and make sure that things like square brackets and angle brackets come through as intended.
Second, consider a different approach. From the data samples you provided, it appears that there is no "key" field that serves to cross-reference the "parent" file with the "child" file. That means you need to be confident about how these two files were created: for each line in the parent file with "C" in the fifth field, exactly one line is written to the "child" file. If this is accurate, then a workable approach could go like this:
- Open the "child" file, read all its contents into an array, and close that file.
- Now, open the "parent" file; for each record that contains a "C" in the fifth field, use perl's "shift" function to take the next entry off the array of "child" records, and append this to the current "parent" record.
You shouldn't have to worry about memory constraints unless the "child" file has hundreds of megabytes of content. The code could end up looking like this:
use strict;
open( IN, "ICD_MEAS_child.txt" ) or die "failed to open child data: $!
+\n";
my @children = <IN>; # read them all at once;
close IN;
open( IN, "ICD_MEAS.txt" ) or die "failed to open parent data: $!\n";
while (<IN>) {
if ( /,C,/ ) {
my $child = shift @children;
s/\n/,$child/; # update: added the essential comma
s/,/;/g; # update: added this line, as per orig. post
}
print;
}
close IN;
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.