in reply to Re: count repeatation
in thread count repeatation
One could fix that by giving split third argument.my $string = "XXXX"; my @count = split( "XX", $string ); print $#count; # Prints -1
But it doesn't scale to generic patterns. Suppose you want to count how after characters are repeated (so, count not only "XX", but also "AA", "22", "--", etc). A pattern /(.)\1/ matches that, but
The reason is that if the pattern in contains capturing parenthesis, split returns what's captured in the parens as well.my $string = "XXYY"; my @count = split( /(.)\1/, $string, -1 ); print $#count; # Prints 4, not 2.
|
|---|