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

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

by jmcnamara (Monsignor)
on Mar 21, 2001 at 18:03 UTC ( #66019=note: print w/replies, xml ) Need Help??


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


Not really shorter just an alternative. This is in golf mode, therefore no -w or strict:
#!/usr/bin/perl sub indexArray { $i = -1; undef %h; map {$h{$_} = $i++} @_; $h{$_[0]} } 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

John.
--

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://66019]
help
Chatterbox?
[Corion]: Meh. Some Open Source people get bought out by advertising firms to change their projects to include advertising. But nobody has approached me to buy out WWW::Mechanize ::$browser from me, to make the browsers launch an ad page ...
[Corion]: ... instead of about:blank, which the modules currently do at startup.
[Corion]: Maybe I'm too pricey. But I haven't received any offers at all ;-)

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2017-07-24 14:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (354 votes). Check out past polls.