in reply to Re: finding index number in an array
in thread finding index number in an array

The additional bonus of List::Util::first() is that it is compiled, so you will get performance akin to grep.

I wrote a myfirst using for that shortcircuits like List::Util:

Rate myfirst first myfirst 875/s -- -83% first 5236/s 498% --

More interesting, possibly, is a comparison of grep and first solutions - does the slight overhead of calling first (a compiled, but still user, function) out-weight the benefit?

my ( $res ) = grep { $array[$_] =~ m/foo/ } 1..$#array; # versus my $res = first { $array[$_] =~ m/foo/ } 1..$#array; __END__ Rate grep first grep 227/s -- -59% first 556/s 144% --

I used 1000 item lists created in each loop with a random item set to foo each time. Obviously the smaller the list, the smaller the return, but even down at a listsize of 10, there was a 7-12% gain...

Okay, it turned out not to be that interesting.