in reply to How to get the correct count
The immediate problem is that * is greedy - it matches as much as it can so it gobbles up everything from the start of the first gene to the end of the last gene. The solution is to use the ? modifier to make it non-greedy: /[AG]TG.*?T[AG][AG]/ig. However there is room for a little more tidying up of your script. Consider:
use strict; use warnings; my $seq = 'CCATGNNNTAACCNNATGNNTAGCC'; $seq =~ s/\s//g; # Count number of bases & regular expressions my $bases = length ($seq); my $A = () = $seq =~ /A/ig; my $GENE = () = $seq =~ /[AG]TG.*?T[AG][AG]/ig; print <<REPORT; Number of bases (including N): $bases. A= $A; GENE(S)= $GENE; REPORT
Number of bases (including N): 25. A= 5; GENE(S)= 2;
The array is not needed at all as you only assign a single value to it, but in any case you should not use the same name for different variables (the array @k and the scalar $k are different variables) because you will very likely cause confusion that way - confusing scripts are hard to maintain.
The = () = trick puts the regular expression in list context and allows the assignment to effectively directly assign the count of matches to a scalar.
The <<REPORT in the print introduces a here doc. The following lines up to the matching REPORT behave as a string that effectively replaces <<REPORT.