while ( my $input = ){ my ( $id2 ) = split /;/, $input; print OUTFILE $input print OUTFILE ';' . $file1_row_for{$id2} if defined $file1_row_for{$id2} print OUTFILE "\n"; }