<?xml version="1.0" encoding="windows-1252"?>
<node id="66019" title="Re: Shortest -return index of element in array- sub" created="2001-03-21 13:03:38" updated="2005-08-02 05:01:11">
<type id="11">
note</type>
<author id="45414">
jmcnamara</author>
<data>
<field name="doctext">
&lt;br&gt;
Not really shorter just an alternative. This is in golf mode, therefore no &lt;tt&gt;-w&lt;/tt&gt; or &lt;tt&gt;strict&lt;/tt&gt;:
&lt;code&gt;
    #!/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

&lt;/code&gt;
&lt;br&gt;
John.
&lt;br&gt;
-- &lt;br&gt;&lt;br&gt;</field>
<field name="root_node">
66003</field>
<field name="parent_node">
66003</field>
</data>
</node>
