in reply to Re: Weird "soundex" algorithm
in thread Weird "soundex" algorithm

Nice. I was unhappy with your vowel stripping though. You use a looping match for offsets into $_ but really, you only have to find the *first* vowel and then no looping is required.

# m/[aeiou]/g and substr($_, pos) =~ s/[aieuo]//g; /[aeiou]/ and substr( $_, $+[0] ) =~ tr/aeiou//d;

Replies are listed 'Best First'.
Re: Re: Re: Weird "soundex" algorithm
by broquaint (Abbot) on Aug 28, 2003 at 23:58 UTC
    You use a looping match for offsets into $_ but really ...
    Er, what looping? The /g matches the first vowel then saves the position of the match for substr which the replace then operates on. I didn't want to use the $+ variable because of the overhead it invokes.
    HTH

    _________
    broquaint

      Er... what overhead? You mean of making an array access because that's all it is. @+ and @- don't invoke the $`, $& and $' penalties. Those arrays are just offsets into the string. $-[0] is the offset of the beginning of the string and $+[0] is the offset of the end of the string. Using those doesn't prompt perl to do all the copying that capturing, $`, $&, and $' do. Its just not the same thing.

      Granted, I did miss that scalar /g loops only once and in this usage that'd be the only loop ever used. I find myself avoiding pos() after learning that it doesn't survive a local() on the variable in question. That's just style though.