in reply to Matching First Character of Strings Efficiently

substr is usually pretty fast:
my $str_a = 'Foo'; my $letter = substr $str_a, 0, 1; for my $str_b ( @list ) { next if $letter eq substr $str_b, 0, 1; expensive_function($str_a , $str_b); }

-Mark

Replies are listed 'Best First'.
Re: Re: Matching First Character of Strings Efficiently
by bageler (Hermit) on Mar 15, 2004 at 20:38 UTC
    why not combine the two commands in the loop?
    my $str_a = 'Foo'; my $letter = substr $str_a, 0, 1; for my $str_b ( @list ) { unless ($letter eq substr $str_b,0,1) { expensive_function($str_a +, $str_b); } }
    update: unless eq is going to be slightly better than if ne

      Well, if this is in a tight loop, then you might gain a bit by not building a scope each time through the loop:

      expensive_function($str_a , $str_b) unless ($letter eq substr $str_b,0 +,1);

      IOW, get rid of the curlies. I'd be curious to find out if this really has any significant effect...

      Or even...

      expensive_function($str_a , $_) unless ($letter eq substr $_,0,1) for @list;

      ... if we are free to play with $_ (otherwise predeclaring $str_b will work too).