<?xml version="1.0" encoding="windows-1252"?>
<node id="66003" title="Shortest -return index of element in array- sub" created="2001-03-21 10:55:06" updated="2005-08-02 05:01:35">
<type id="1042">
CUFP</type>
<author id="11967">
leons</author>
<data>
<field name="doctext">
Hi monks,&lt;BR&gt;
&lt;BR&gt;
My friend and colleague ChOas and I were having a little contest&lt;BR&gt;
here. We dared each other to write the shortest sub, returning the&lt;BR&gt;
index-number of the supplied element in an array&lt;BR&gt;
We came up with the following ...
&lt;BR&gt;
&lt;CODE&gt;
#!/usr/bin/perl -w

sub indexArray($@)
{
 my $s=shift;
 $_ eq $s &amp;&amp; return @_ while $_=pop;
 -1;
}

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
&lt;/CODE&gt;
&lt;BR&gt;
Is anyone bored enough to participate in this silly contest?&lt;BR&gt;
Anything, shorter, funnier, more obfuscated ... will do and&lt;BR&gt;
is welcome ;-)&lt;BR&gt;
&lt;BR&gt;
If you do not want to participate because you think this is too&lt;BR&gt;
silly and a waste of your time, then please don't, cause you're&lt;BR&gt;
right ! ;-)&lt;BR&gt;
&lt;BR&gt;
Bye, Leon</field>
</data>
</node>
