Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

John.
--

Update: Just saw ChOas' post.


Comment on Re: Shortest -return index of element in array- sub
Download Code
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
Node Status?
node history
Node Type: note [id://66030]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (15)
As of 2014-10-31 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (221 votes), past polls