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

by BigVic (Initiate)
 on Apr 26, 2001 at 03:44 UTC 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; [download]``` 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; [download]``` 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"; [download]``` 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; [download]``` Replace ( \$index ) with an array to match multiple instances. 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"; [download]``` 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 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; } [download]``` 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"; [download]```

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
 [LanX]: hmm there is an xml ticker for threads

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2018-03-19 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (238 votes). Check out past polls.

Notices?