Go on, spoil the mystique :)

Even fixing up the out-by-two error s/1+pos($$s)/pos($$s)-1/, I can't see how to correct the pos memory factor without explicitly resetting it? Which makes taking the reference and localising it redundant?

#! perl -slw use strict; sub tye { my $s= \shift @_; my( $p, $n )= @_; local( pos($$s) ); 0 while $$s =~ /\Q$p/g && 0 < --$n; return if 0 < $n; return 1+pos($$s); } my $data = 'a' x 100; print "want: got"; printf "%3d : %3d\n", $_-1, tye( $data, 'a', $_ ) for 1 .. 30; __END__ C:\test>junk want: got 0 : 2 1 : 4 2 : 7 3 : 11 4 : 16 5 : 22 6 : 29 7 : 37 8 : 46 9 : 56 10 : 67 11 : 79 12 : 92 Use of uninitialized value in printf at C:\test\junk.pl line 16. 13 : 0 14 : 16 15 : 32 16 : 49 17 : 67 18 : 86 Use of uninitialized value in printf at C:\test\junk.pl line 16. 19 : 0 20 : 22 21 : 44 22 : 67 23 : 91 Use of uninitialized value in printf at C:\test\junk.pl line 16. 24 : 0 25 : 27 26 : 54 27 : 82 Use of uninitialized value in printf at C:\test\junk.pl line 16. 28 : 0 29 : 31

About the best performing modification of it I came up with is:

sub tyem { my( $s, $c, $n ) = @_; 0 while $s =~ /\Q$c/g && 0 < --$n; my $rv = pos( $s )-1; pos($s) = -1; return if 0 < $n; return $rv; }

which doesn't fair so well, though it is still quicker than any of the other regex-based solutions.


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.

In reply to Re^2: The nth occurrence of a character (simple /$c/g) by BrowserUk
in thread The nth occurrence of a character by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.