Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Is there an elegant way of resetting the match position?

by JadeNB (Chaplain)
on Oct 22, 2009 at 21:45 UTC ( [id://802807]=note: print w/replies, xml ) Need Help??


in reply to Is there an elegant way of resetting the match position?

The following seems to me to be linear-time *, as opposed to your algorithm, which looks quadratic (but eye-balling running times is notoriously unreliable, especially for me!):

my @runs; my $pos = 0; my $b = $a; my $c = lc substr $b, 0, 1, ''; my $next; while ( $b ) { my $start = $pos; 1 while ++$pos and ( $next = lc substr $b, 0, 1, '' ) eq $c; for my $i ( $start .. $pos - $n ) { push @runs, substr $a, $i, $n; } $c = $next; }

UPDATE: Wow, I completely misread! My original version just printed out all runs of a repeated character. I've fixed it now.
* At the expense of not using regexes, which you specifically requested. Oh, well. Is there a reason that you prefer to use them?
UPDATE 2: Whoops, I was eating one character too many each pass through the loop.
UPDATE 3: OK, now I've actually tested it. Sorry about that. :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://802807]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-03-29 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found