Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by duelafn (Priest)
on Mar 24, 2001 at 15:25 UTC ( #66855=note: print w/ replies, xml ) Need Help??


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

What if we want to find all of the values? The best I could do was this,

sub find { 1 while $_[0]ne pop; $#_+1?(&a(@_),$#_):(); } my @array=('john','paul','ringo','george', 'ringo'); print &find('john',@array),"\n"; # Returns 0 print &find('ringo',@array),"\n"; # Returns 24 print &find('mick',@array),"\n"; # Returns undef
Good Day,
    Dean


Comment on Re: Shortest -return index of element in array- sub
Download Code
Re: Re: Shortest -return index of element in array- sub
by MeowChow (Vicar) on Mar 24, 2001 at 23:59 UTC
    sub i { pop eq $_[0] ? @_-1 : (), $#_ ? &i : () }
    Ahhhh, more recursion...

    ps. This is why I used @_-1 instead of $#_

Re: Re: Shortest -return index of element in array- sub
by MrNobo1024 (Hermit) on Apr 02, 2001 at 04:41 UTC
    sub aindex { grep$_[$_+1]eq$_[0],0..@_-2 }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2015-07-06 07:40 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 (70 votes), past polls