tags:

views:

197

answers:

4

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() ]

+21  A: 
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)

hobbs
This is definitely the way to go, 9 times out of 10.
Zaid
Why is this better than your solution, Zaid?
masonk
@masonk : Backwards-compatibility for one. Also, `first` will exit the implicit loop upon finding the index that matches. The `grep` equivalent would be `$idx = grep { $array[$_] eq 'whatever' and last } 0 .. $#array;`, a bit hairy for my liking. And then it's miles ahead in the speed-race, when run as `List::Util::XS`.
Zaid
Just realized you can't `last` out of `grep` cleanly. Whoops!
Zaid
+1  A: 

You can write a function for this:

sub array_search {
    my ($arr, $elem) = @_;
    my $idx;
    for my $i (0..$#$arr) {
        if ($arr->[$i] eq $elem) {
            $idx = $i;
            last;
        }
    }
    return $idx;            
}

The index of the first matching element will be returned, or undef.

eugene y
My final solution: sub array_search { my ($element, @array) = @_; foreach (0..$#array) { if ($array[$_] eq $element) { return $_; } } return -1; }
powerboy
+3  A: 

Here's a post-5.10 way to do it, with the added benefit of determining how many indexes match the given value.

my @matches = grep { $array[$_] ~~ $element } 0 .. $#array;

If all elements are guaranteed to be unique, or just the first index is of interest:

my ($index) = grep { $array[$_] ~~ $element } 0 .. $#array;
Zaid
That answer does not fit the question. The index is what goes into the square brackets for array access, so: a number between `0` and `$#array`.
daxim
@daxim : Aren't we after the index of the match(es) here?
Zaid
Actually, the answer does fit the question, and very nicely.
masonk
+5  A: 

Here is an autobox solution:

use autobox::Core;

my @things = qw/blue black green red green yellow/;

my $first_green = @things->first_index( sub{ $_[0] eq 'green' } ); # code block
my $last_green  = @things->last_index ( qr/^green$/ );             # or regex

say $first_green;    # => 2
say $last_green;     # => 4

/I3az/

draegtun
upvote for autobox hilariousness
masonk