Does Perl have a build-in function to get the index of an element in an array? Or I need write such a function by myself? [ equivalent to PHP array_search() or JavaScript array.indexOf() ]
use List::Util qw(first); $idx = first { $array[$_] eq 'whatever' } 0..$#array;
(List::Util is core)
or
use List::MoreUtils qw(firstidx); $idx = firstidx { $_ eq 'whatever' } @array;
(List::MoreUtils is on CPAN)
2.1m questions
2.1m answers
60 comments
57.0k users