in reply to Idiomatic Array Index Search?

I liked all these solutions so much that I couldn't resist benchmarking a few of them to help me choose my favorite. Thought I'd share the results in case any of you also like benchmarks.

      ar0n:  4 wallclock secs ( 5.30 usr +  0.00 sys =  5.30 CPU) @ 1826.79/s (n=9682)
clever_tachyon:  4 wallclock secs ( 5.01 usr +  0.00 sys =  5.01 CPU) @ 605.19/s (n=3032)
     dkubb:  6 wallclock secs ( 5.22 usr +  0.00 sys =  5.22 CPU) @ 1821.65/s (n=9509)
   footpad:  5 wallclock secs ( 5.34 usr +  0.00 sys =  5.34 CPU) @ 385.96/s (n=2061)
     japhy:  6 wallclock secs ( 5.21 usr +  0.00 sys =  5.21 CPU) @ 2507.87/s (n=13066)
    merlyn:  6 wallclock secs ( 5.25 usr +  0.00 sys =  5.25 CPU) @ 1056.57/s (n=5547)
   tachyon:  6 wallclock secs ( 5.59 usr +  0.00 sys =  5.59 CPU) @ 1766.01/s (n=9872)
I have edited this a few times since I didn't get my post perfect the first time
use strict; use Benchmark 'timethese'; my @array; my $lookfor; my @array = qw ( Juan Montoya Buddy Lazier Eliseo Salazar Jeff Ward Ed +die Cheever Robby Gordon Jimmy Vasser Stephan Gregoire Scott Goodyear Scott Sharp +Mark Dismore Donnie Beechler Jaques Lazier Jeret Schroeder Billy Boat Raul Boesel J +ason Leffler Buzz Calkins Steve Knapp Davey Hamilton Robby McGehee Johnny Unser Sta +n Wattles Sam Hornish Jr Airton Dare Robbie Buhl Richie Hearn Andy Hillenburg Al + Unser Jr Jimmy Kite Sarah Fischer Lyn St. James Greg Ray ); $lookfor= "toma"; test_get_index($lookfor); $lookfor= $array[21]; test_get_index($lookfor); + sub test_get_index { my ($lookfor) = @_; print "Looking for $lookfor\n", "dkubb found ",get_index_dkubb(\@array, $lookfor),"\n", "merlyn found ",get_index_merlyn(\@array, $lookfor),"\n", "japhy found ",get_index_japhy(\@array, $lookfor),"\n", "clever tachyon found ",get_index_clever_tachyon($lookfor, @arra +y),"\n", "tachyon found ",get_index_tachyon($lookfor, @array),"\n", "footpad found ",get_index_footpad($lookfor, @array),"\n", "ar0n found ",get_index_ar0n($lookfor, @array),"\n"; } timethese(-5, { dkubb => sub {get_index_dkubb(\@array, $lookfor)}, merlyn => sub {get_index_merlyn(\@array, $lookfor)}, japhy => sub {get_index_japhy(\@array, $lookfor)}, clever_tachyon => sub {get_index_clever_tachyon($lookfor, @array)}, tachyon => sub {get_index_tachyon($lookfor, @array)}, footpad => sub {get_index_footpad($lookfor, @array)}, ar0n => sub {get_index_ar0n($lookfor, @array)}, }); sub get_index_dkubb { my ($a, $e) = @_; $e eq $a->[$_] and return $_ for 0..$#$a; return -1; } sub get_index_merlyn (\@$) { my ($a, $e) = @_; for (my $i = 0; $i <= $#$a; $i++) { return $i if $e eq $a->[$i]; } return -1; } sub get_index_japhy (\@$) { my ($a, $e) = @_; my $i = 0; for (@$a) { return $i if $e eq $_; $i++; } return -1; } sub get_index_tachyon { my $value = shift; for my $index(0..$#_) { return $index if $value eq $_[$index]; } return -1; } sub get_index_clever_tachyon { map{return --$_ if $_[0] eq $_[$_]}(1..@_-1) and -1 } sub get_index_footpad { my $value = shift; my @values = @_; my $result = -1; for ( my $index = 0; $index < @values; $index++ ) { if ( $value eq $values[ $index ] ) { $result = $index; last; } } return $result; } sub get_index_ar0n { my $value = shift; for (0..$#_) { return $_ if $_[$_] eq $value; } return -1; }
It should work perfectly the first time! - toma