while(my $pline=) { chomp $pline; $parrecord = $pline; my @parfields = split(); ... } #### CHILD: foreach (@carray) { # search line-by-line for a match of $_ to field #5 # if each field#5 in is guaranteed unique, you can go to the next CHILD element once a match is found } #### my %lookupHash = (); while(my $line=) { chomp $line; my @fields = split(/\|/, $line); $lookupHash{ $fields[1] } = (); # $field[1] = 2nd field #$lookupHash{ $fields[1] } = $line; # need entire line? } #### while(my $line=) { chomp $line; my @fields = split(/\|/, $line); print $OUTFILE $line."\n" if(exists $lookupHash{ $fields[4] }); } #### field pre suf $HoH{$pre}->{$suf} = () ---------------- ---- ----------------- ------------------------ 1400000042597061="1" +"400000042597061" $HoH{1}->{400000042597061}=(); ="14"+"00000042597061" $HoH{14}->{00000042597061}=(); ... #### field prefix suffix $HoH{$suf}->{$pre} = (); ---------------- ----------- -------- ------------------------- 1400000042597061="1400000042"+"597061" $HoH{597061}->{1400000042}=(); #### my $prefix = substr($myField, 0, 11); my $suffix = substr($myField, 11); #### my %lookupHash = (); LINE: while(my $line=) { chomp $line; my @fields = split(/\|/, $line); my $prefix = substr($fields[1], 0, 11); # Grab from pos0 to pos9 my $suffix = substr($fields[1], 11); # Grab from pos10 and on $lookupHash{$suffix}->{$prefix} = (); #$lookupHash{$suffix}->{$prefix} = $line; } #### LINE: while(my $line=) { chomp $line; my @fields = split(/\|/, $line); # do suffix first; skip line in parent if suffix absent my $suffix = substr($fields[4], 11); next LINE if(!exists $lookupHash{$suffix}); # reach here if suffix exists; check prefix my $prefix = substr($fields[4], 0, 11); next LINE if(!exists $lookupHash{$suffix}->{$prefix}); # Output the parent line print $OUTFILE $line."\n"; # ... or output the child line if you needed that one #print $OUTFILE $lookupHash{$suffix}->{$prefix}."\n"; }