See perlrequick.
Your regexp is capturing into $1 just one character, since you are using a character class. It is looking for:
So, ignoring the fact that you have two variable $last_char and $last_mapped_char, it makes sense that you get a result, since you will have captured the last, last occurrence with your regexp, because it's greedy and the first part of it will eat up all the letters except for the last one. It's probably not how you want to code it, though.[ATCG]+ # one or more of any of A,T,C,G # followed by ([ATCG]) # exactly one of A,T,C,G # which is captured into $1 # followed by \.+ # one or more dots # followed by $ # the end of the string
Try running some tests:
perl -Mstrict -wE ' my $str = "ATCGATCG..."; if($str=~/[ATCG]+([ATCG])\.+$/) { my $last_char=$1; say $last_char; my $pos = rindex($str, $last_char); say $pos; } ' G 7
perl -Mstrict -wE ' my $str = "ATCGATCGA..."; if($str=~/[ATCG]+([ATCG])\.+$/) { my $last_char=$1; say $last_char; my $pos = rindex($str, $last_char); say $pos; } ' A 8
Hope this helps!
In reply to Re^3: Proper usage of rindex function?
by 1nickt
in thread Proper usage of rindex function?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |