Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Shortest -return index of element in array- sub

by Tyke (Pilgrim)
on Mar 21, 2001 at 18:39 UTC ( #66027=note: print w/ replies, xml ) Need Help??


in reply to Shortest -return index of element in array- sub

How about

sub indexArray{for(1..@_){$_[0]eq$_[$_]&&return$_-1}-1} my @array=('john','paul','ringo','george'); print indexArray('john',@array) ."\n"; # Returns 0 print indexArray('ringo',@array) ."\n"; # Returns 2 print indexArray('mick',@array) ."\n"; # Returns -1
retaining jmcnamara comments about golf


Comment on Re: Shortest -return index of element in array- sub
Download Code
Re (tilly) 1: Shortest -return index of element in array- sub
by tilly (Archbishop) on Mar 22, 2001 at 05:57 UTC
    If the contest is for an index and not the reverse index, then the above solution is correct. But the offerings that use pop will find the last match in the array rather than the first.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://66027]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2014-07-26 17:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (178 votes), past polls