use strict; use Benchmark 'timethese'; my @array; my $lookfor; my @array = qw ( Juan Montoya Buddy Lazier Eliseo Salazar Jeff Ward Eddie Cheever Robby Gordon Jimmy Vasser Stephan Gregoire Scott Goodyear Scott Sharp Mark Dismore Donnie Beechler Jaques Lazier Jeret Schroeder Billy Boat Raul Boesel Jason Leffler Buzz Calkins Steve Knapp Davey Hamilton Robby McGehee Johnny Unser Stan 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, @array),"\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; }