Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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

by jmcnamara (Monsignor)
on Mar 21, 2001 at 19:00 UTC ( #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 } }


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} }
      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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://66030]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2018-06-19 01:27 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.