in reply to Re: quickly counting substrings
in thread quickly counting substrings
That routine is a little broken -- the return value of index() can be 0 on success, and you'll want to set $pos to the current position plus whatever the length of the separator is at each iteration.
One version of using index() that fares somewhat comparably to the m_while_M routine is:
my $_index = sub { my $cnt = 0; local $[ = 1; # ook! my $pos = -1; ++$cnt while $pos = index($dta,"\n\n",$pos+2); $cnt{_index} = $cnt; };
But an alternate version of the m_while_M routine (using while as a statement modifier rather than the block form) seems to be the best I can come up with at this hour:
my $_while_re = sub{ my $cnt = 0; my $recsep = "\n\n"; $cnt++ while $dta =~ /$recsep/g; $cnt{_while_re} = $cnt; };
|
|---|