in reply to The nth occurrence of a character

my $char = "C"; my $pat = "[^$char]*$char" x $n; my $where; /^($pat)/ and $where = length $1 - 1;

Replies are listed 'Best First'.
Re^2: The nth occurrence of a character
by japhy (Canon) on Feb 08, 2006 at 12:53 UTC
    You could go one step further and use /^$pat/ and $where = $+[0]-1, but that makes use of the @+ array, and I'd probably leave a comment on that line.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

      This is certainly cheaper. If capturing can be avoided and this code is important for speed, choose the non-capturing version.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊