in reply to Idiomatic Array Index Search?

Notes: That said, I would take an approach like:
# prototype here for "look and feel good"ness # if you remove it, be sure to pass array by # reference (this is done for speed reasons) # $index = get_index @array, $element; sub get_index (\@$) { my ($a, $e) = @_; my $i = 0; for (@$a) { return $i if $e eq $_; $i++; } return -1; }
There's no reason to get more idiomatic, because it will cloud the meaning of your code. The most simple things should be coded simply.

japhy -- Perl and Regex Hacker
  • Comment on Simple things should be coded simply (Re: Idiomatic Array Index Search?)
  • Download Code

Replies are listed 'Best First'.
Re: Simple things should be coded simply (Re: Idiomatic Array Index Search?)
by merlyn (Sage) on May 28, 2001 at 20:33 UTC
    sub get_index (\@$) { my ($a, $e) = @_; my $i = 0; for (@$a) { return $i if $e eq $_; $i++; } return -1; }
    I don't like code like this because you've got two things in that loop that are being kept in sync only coincidentally: the iteration of the foreach, and the incrementing of the index. I'd let there be one thing, and use the indirection to ensure consistency:
    sub get_index (\@$) { my ($a, $e) = @_; for (my $i = 0; $i <= $#$a; $i++) { return $i if $e eq $a->[$i]; } return -1; }
    This is clearer to me. Simple things should be coded simply. {grin}

    -- Randal L. Schwartz, Perl hacker

Re: Simple things should be coded simply (Re: Idiomatic Array Index Search?)
by mattr (Curate) on May 28, 2001 at 18:57 UTC
    I agree with all the above (simple things simple etc.) but am wondering what happens if you have two matching elements. I don't remember anybody saying they were unique, and an array just looks like a set when you use qw() but it isn't one mathematically.

    So maybe you should return an array, sorted of course.

    Then may I propose the most idiomatic would be to use another language, SQL of course? I am thinking of DBD::RAM. You could say something like

    print $dbh->selectcol_arrayref(qq [ SELECT phrase FROM my_phrases WHERE id = 1 ORDER BY phrase ])->[0];

    Some other neat looking modules may include Search::InvertedIndex, Search::Binary, and Math::IntervalSearch (Search where an element lies in a list of sorted elements).

    ..to answer japhy and merlyn below, honored to join this thread with you both. To be sure, grep is good. Thank you for your elegant code.

      Then grep() would be a super choice:
      sub get_index { my ($a, $e) = @_; my @idx = grep $a->[$_] eq $e, 0 .. $#$a; return wantarray ? @idx : $idx[0]; }


      japhy -- Perl and Regex Hacker