In the worst case, index will crawl the entirety of $str_a looking for the character you extracted from $str_b so it can't be the fastest solution.
You want to make sure you only make a single one-character comparison. And because one of the strings is invariant during the loop, you want to move its first-character extraction out of the loop. That gives us the canonical solution of
my $str_a = 'Foo'; my $chr_a = substr $str_a, 0, 1; for my $str_b ( @list ) { next if $chr_a eq substr($str_b, 0, 1); expensive_function($str_a , $str_b); }
Update: beaten to the punch by kvale.
Makeshifts last the longest.
In reply to Re: Matching First Character of Strings Efficiently
by Aristotle
in thread Matching First Character of Strings Efficiently
by Limbic~Region
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |