Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How do I find the index of a specific array value?

( #75660=categorized question: print w/ replies, xml ) Need Help??
Contributed by BigVic on Apr 26, 2001 at 03:44 UTC
Q&A  > arrays


Answer: how do i find the index of a specific array value?
contributed by merlyn

my @array = qw( your array here ); my $search_for = "here"; my( $index )= grep { $array[$_] eq $search_for } 0..$#array;
Answer: how do i find the index of a specific array value?
contributed by hdp

For very large arrays where bailing out as soon as a match is found is a win:

my @a = ( 1 .. 1_000_000 ); # some large array my $want = 5843; my $index = 0; ++$index until $a[$index] == $want or $index > $#a;
Answer: how do i find the index of a specific array value?
contributed by I0

You could use an index hash:

my @array = qw( your array here ); my $search = "array"; my %index; @index{@array} = (0..$#array); my $index = $index{$search}; print $index, "\n";
This is a win, for larger arrays, if you need to do multiple/many lookups while the array remains static.
Answer: How do I find the index of a specific array value?
contributed by marcussen

Using merlyn's example with regular expressions, so you don't need to know the exact value of the element you are matching;

my @array = ( 'Name: Mr. Jones', 'Phone: 555-555', 'Email: jones@example.com' ); my ( $index )= grep { $array[$_] =~ /Phone/ } 0..$#array;
Replace ( $index ) with an array to match multiple instances.

Answer: how do i find the index of a specific array value?
contributed by MeowChow

You could use my aindex and raindex.

Answer: How do I find the index of a specific array value?
contributed by snoopy

I like List::MoreUtils:

use List::MoreUtils; my @array = qw( Apples Oranges Brains Toes Kiwi); my $search = "Toes"; my $index = List::MoreUtils::first_index {$_ eq $search} @array; print "index of $search = $index\n";
Answer: How do I find the index of a specific array value?
contributed by simul

If the array is large and sorted, you might want search it more efficiently:

my @array = qw( your large, sorted array here ); my $search = "thing"; my $index = bsearch(\@array, $search); sub bsearch { my ($array, $word) = @_; my $low = 0; my $high = @$array - 1; while ( $low <= $high ) { my $try = int( ($low+$high) / 2 ); $low = $try+1, next if $array->[$try] lt $word; $high = $try-1, next if $array->[$try] gt $word; return $try; } return; }
Answer: How do I find the index of a specific array value?
contributed by myuserid7

You should use first() from List::Util. It is a core module, unlike List::MoreUtils and other modules mentioned above, so it is portable and can be used with no extra effort.

use List::Util qw(first); my @array = qw( Apples Oranges Brains Toes Kiwi); my $search = "Toes"; my $index = first { $array[$_] eq $search } 0 .. $#array; print "index of $search = $index\n";

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (8)
    As of 2015-07-31 02:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (274 votes), past polls