in reply to The nth occurrence of a character

my $str = 'one a two a three a four'; my $char = 'a'; my $n = 2; $str =~ /(?:.*?$char){$n}/g; print "Pos of '$char' #$n in '$str' is ", pos($str), "\n";

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: The nth occurrence of a character
by BrowserUk (Patriarch) on Feb 08, 2006 at 21:30 UTC

    You have to make sure that you reset pos($str) if you use this more than once otherwise it continues from the last match.

    However, for reasons that I cannot explain, this method steadfastly refuses to match beyond position 21165. Ask for any match after that and (on my system) the script quietly self terminates. No segault. No panic. No message at all.

    Is this a bug worthy of reporting or am I missing something here?

    #! perl -w use strict; my $str = 'a' x 100000; my $char = 'a'; for my $n ( map{ $_ * 1000 + 165 } 1 .. 30 ) { pos($str)=0; $str =~ /(?:.*?$char){$n}/g; print "Pos of '$char' #$n in \$str is ", pos($str), "\n"; } __END__ C:\test>junk Pos of 'a' #1165 in $str is 1165 Pos of 'a' #2165 in $str is 2165 Pos of 'a' #3165 in $str is 3165 Pos of 'a' #4165 in $str is 4165 Pos of 'a' #5165 in $str is 5165 Pos of 'a' #6165 in $str is 6165 Pos of 'a' #7165 in $str is 7165 Pos of 'a' #8165 in $str is 8165 Pos of 'a' #9165 in $str is 9165 Pos of 'a' #10165 in $str is 10165 Pos of 'a' #11165 in $str is 11165 Pos of 'a' #12165 in $str is 12165 Pos of 'a' #13165 in $str is 13165 Pos of 'a' #14165 in $str is 14165 Pos of 'a' #15165 in $str is 15165 Pos of 'a' #16165 in $str is 16165 Pos of 'a' #17165 in $str is 17165 Pos of 'a' #18165 in $str is 18165 Pos of 'a' #19165 in $str is 19165 Pos of 'a' #20165 in $str is 20165 Pos of 'a' #21165 in $str is 21165

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Looks to be a version-specific limitation. Mine (ActivePerl) stops somewhere between 31200 and 31300. Interesting find. Since it quietly aborts, I'd say it's worth a bug report.

      Caution: Contents may have been coded under pressure.