in reply to Matching First Character of Strings Efficiently

A summary of all solutions...
Here there's a benchmarked code:
#!/usr/bin/perl use strict; use warnings; use Benchmark qw(:all); cmpthese(-3, { 'Original' => \&test1, 'kral' => \&test2, 'BrowserUk' => \&test3, 'kvale' => \&test4, } ); my @list = qw( pippo pluto foo Foo ); sub test4 { 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); } } sub test3 { my %list; @list{ map{ substr $_, 0, 1 } @list } = (); my $str_a = 'Foo'; my $first_char = substr $str_a, 0, 1; for my $str_b ( @list ) { next if exists $list{ $first_char }; expensive_function($str_a , $str_b); } } sub test2{ my $str_a = 'Foo'; my $letter = substr $str_a, 0, 1; for my $str_b ( @list ) { next if (ord($letter) == ord((split(//, $str_b))[0])); expensive_function($str_a , $str_b); } } sub test1 { my $str_a = 'Foo'; # Can be any len +gth for my $str_b ( @list ) { # Can be any num +ber of items next if index($str_a, substr($str_b, 0, 1)); # Only change this expensive_function($str_a , $str_b); } }
BrowserUk's solution seems to be the best!
Update: I miss the Limbic~Region post, sorry...
----------
kral
(sorry for my english)