Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
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.
--