... open (ERRORFILE,"<", "Errors-email.txt") || die "File not found\n"; @error_array = ; ... foreach my $line(@error_array){ # # Some $line substitutions here... ... push(@sample_ids,$line); ... #### ... foreach my $records(@merged_array){ if($records =~ /$sample_ids[3]/){ ...