Suppose I have:
my $string = \"one.two.three.four\";
How should I play with context to get the number of times the pattern found a match (3
I noticed that if you have an OR condition in your regular expression (eg /(K..K)|(V.AK)/gi ) then the array produced may have undefined elements which are included in the count at the end.
For example:
my $seq = "TSYCSKSNKRCRRKYGDDDDWWRSQYTTYCSCYTGKSGKTKGGDSCDAYYEAYGKSGKTKGGRNNR";
my $regex = '(K..K)|(V.AK)';
my $count = () = $seq =~ /$regex/gi;
print "$count\n";
Gives a value of count of 6.
I found the solution in this post How do I remove all undefs from array?
my $seq = "TSYCSKSNKRCRRKYGDDDDWWRSQYTTYCSCYTGKSGKTKGGDSCDAYYEAYGKSGKTKGGRNNR";
my $regex = '(K..K)|(V.AK)';
my @count = $seq =~ /$regex/gi;
@count = grep defined, @count;
my $count = scalar @count;
print "$count\n";
Which then gives the correct answer of three.