in reply to Re: Re: Re: Re: Multiple text files
in thread Multiple text files

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:

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;