Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by jmcnamara (Monsignor)
on Mar 21, 2001 at 19:00 UTC ( [id://66030]=note: print w/replies, xml ) Need Help??


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


Or maybe this:
sub indexArray{ while (@_) { return @_-1 if $_[0] eq pop } }

John.
--

Update: Just saw ChOas' post.

Replies are listed 'Best First'.
Re: Re: Shortest -return index of element in array- sub
by mr_mischief (Monsignor) on Apr 04, 2001 at 06:19 UTC
    Darn. This is the route I went, too.
    Just a slight bit of difference.
    I really need to read all the posts before taking a challenge mentioned in the first one. ;-)

    sub indexArray2 { while(@_){$_[0] eq pop && return @_-1} }
    Chris
      Now that I've taken a closer look, this is the shortest iterative version I could find (and also the shortest I could make it and have it run under strictures and warnings, although those aren't really prerequisites in golf):
      sub indexArray3{ 1while$_[0]ne pop;$#_ }
      I'd say that's pretty obfuscated, too. It can be done in a couple fewer characters recursively, if you use a one-character subroutine name and eliminate any unnecessary whitespace. I'd like to know hoe to do it in fewer iteratively, but I just can't figure anything out. Please point it out to me if there's a way.

      Chris Stith

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-16 06:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found