foreach my $l (@file) { if (($l =~ /^>(\w{2})(.{1})(\w{2}\d{6}.{2})(\D?)(\d+)(\W{1})(\d+)/)) { $current = undef; if ($4) { $hit = "$7-$5"; } else { $hit = "$5-$7"; } } foreach my $k (@positions) { if ($k == $hit) { $in_gene = 1; } } if ($in_gene) { $gene .= $l; } unless ($l =~ /^>/) { next; } } print $gene;