in reply to Get Position of Element in an array

this will impress your professor! =)
DB<1> @input=('a','b','c','d'); DB<2> %pos= map { $_=>$i++ } @input DB<3> print $pos{a} 0 DB<4> print $pos{d} 3

... so what if elements aren't unique?

Cheers Rolf

Replies are listed 'Best First'.
Re^2: Get Position of Element in an array
by LanX (Saint) on Nov 16, 2010 at 19:26 UTC
    >... so what if elements aren't unique?

    since this question hasn't been answered yet here a variation for finding the first idx!

    %pos= map { $_=>$i++ } reverse @input;

    If you need it more than once, then the overhead for building a hash will easily pay off.

    Cheers Rolf