in reply to Trying to get index functionality in array context

This actually uses index, so it should run faster than regex based versions. Benchmarking is left as an exercise for the more interested. :)

sub index_a { my( $txt, $search ) = @_; my @ret; my $ind = 0; my $cur = 0; while( ($cur = index( $txt, $search, $ind )) != -1 ) { push @ret, $cur; $ind = $cur + 1; } return @ret; }

Replies are listed 'Best First'.
Re: Bones! Take a reading . . .
by merlyn (Sage) on Jan 28, 2002 at 22:04 UTC
    This actually uses index, so it should run faster than regex based versions.
    Not necessarily. If the text is a simple string, the regex engine can kick in BoyerMoore and other optimizations, and actually beat a naive index operation. Having said that, I believe that modern Perls actually hand index to the regex engine, just so that it can use BoyerMoore!

    -- Randal L. Schwartz, Perl hacker