use strict; open( IN, "ICD_MEAS_child.txt" ) or die "failed to open child data: $!\n"; my @children = ; # read them all at once; close IN; open( IN, "ICD_MEAS.txt" ) or die "failed to open parent data: $!\n"; while () { 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;