if ($probe_ID =~ m/$probe_name[$i]/) #match the current entry with the list of probe names #### # afater the first loop: my $probes_regex = join '|', @probe_name; # you don't need to store all lines in @line, it only wastes memory while (my $line = ) { my @data = split /\t/, $line; if ($data[0] =~ $probes_regex) { print OUT "$gene[$i]\t$line\n"; } else { print "no match\n"; } }