in reply to Re: Matching First Character of Strings Efficiently
in thread Matching First Character of Strings Efficiently

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

Replies are listed 'Best First'.
Re:x3 Matching First Character of Strings Efficiently
by grinder (Bishop) on Mar 15, 2004 at 22:24 UTC

    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).