Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: How do I find the position of an element in an array?

by fundflow (Chaplain)
on Sep 22, 2001 at 13:22 UTC ( [id://114037]=note: print w/replies, xml ) Need Help??


in reply to How do I find the position of an element in an array?

If you need to do this operation many times on an unchanging array, you can use a hash to store the indices. You'd have to re-set the hash any time the array contents changed. You could use an object to encapsulate this mess. :-)
my @array = ( ... ); my %array_element_index; @array_element_index{ @array } = (0 .. $#array); my $i = $array_element_index{ $value_of_interest };
Of course, this method and the repeated-greps method both have their own idiosyncratic costs, so which one is more effective/efficient for you really depends on your situations.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://114037]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-24 19:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found