in reply to Challenge: fastest count of substring

I have been experimenting with various regexes until I observed that the various proposals give different answers plus/minus 1. I assume your challenge would require that the answer is correct, right?

  • Comment on Re: Challenge: fastest count of substring

Replies are listed 'Best First'.
Re^2: Challenge: fastest count of substring
by LanX (Saint) on May 06, 2013 at 21:06 UTC
    Sorry I thought it's obvious, any technique based on separation at delimiters results in an incremented result, so

     $x =split /;/, $string means their are $x-1 ";" in string.

    edit

    IMHO the only chance to get faster than now is to find an obscure feature in unpack.

    update

    example:

    DB<103> $_="aXaXa" => "aXaXa" DB<104> $x=split /X/ => 3 DB<105> split /X/ => ("a", "a", "a")

    ooops! 8-/

    DB<106> $_="XaX" => "XaX" DB<107> $x=split /X/ => 2 DB<108> split /X/ => ("", "a")

    Cheers Rolf

    ( addicted to the Perl Programming Language)