Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: 'grouping' substrings?

by kwaping (Priest)
on Feb 01, 2006 at 15:54 UTC ( [id://527082]=note: print w/replies, xml ) Need Help??


in reply to 'grouping' substrings?

You might like Perl's index function.

Replies are listed 'Best First'.
Re^2: 'grouping' substrings?
by ikegami (Patriarch) on Feb 01, 2006 at 16:50 UTC

    Using index would look something like the following:

    sub using_index { our $seq; *seq = \$_[0]; my @groups; my $pos = -1; my $start = -1; for (;;) { my $new_pos = index($seq, 'M', $pos+1); if ($new_pos < 0) { if (defined($start)) { push(@groups, [ $start, $pos ]); } last; } if ($start < 0) { $start = $new_pos; } elsif ($new_pos - $pos > 1) { push(@groups, [ $start, $pos ]); $start = $new_pos; } $pos = $new_pos; } return @groups; }

    It would be simpler if there was a function that returned the next character which isn't 'M'.

    As you can guess, it's much slower than the regexp approach. The regexp approach is 170% faster than (i.e. 2.7 times the speed of) the index method on the input you provided.

    Benchmark code:

    Benchmark results:

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2024-04-16 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found