in reply to Re: Finding Start/End Position of the Uppercase Substring
in thread Finding Start/End Position of the Uppercase Substring
my $hyphens = 0; $hyphens++ while $str =~ /-/g;
Although the data given seems clean in this regard, your code will give wrong results if there are any hyphens in the string other than leading ones. Capturing zero or more hyphens at the beginning of the string and finding the length of the capture might be safer.
my $hyphens = length $1 if $str =~ m{\A(-*)};
The match will always succeed so if there are no leading hyphens the length of the capture will be zero.
$ perl -Mstrict -Mwarnings -le ' > my @strings = qw{--aacgtACG ctgGTTAtga}; > foreach my $str ( @strings ) > { > my $hyphens = length $1 if $str =~ m{\A(-*)}; > print qq{$str - $hyphens}; > }' --aacgtACG - 2 ctgGTTAtga - 0 $
Cheers,
JohnGG
|
|---|