Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re^3: How to find the index of an element in an array?by sk (Curate) |
on Sep 06, 2005 at 06:58 UTC ( [id://489376]=note: print w/replies, xml ) | Need Help?? |
There are other ways. 0. Making a hash out (my previous post) 1. Loop through the elements and check for the value you are looking for and return the index. This is O(n) and very inefficient if you want to get the index quite frequently and if your array is big 2. You had a,b,c etc. as elements then to find g then you can do something like binary search. This assumes your list/array is ordered. 3. Come up with your own data structure. All these things are related to your original question but if you post your original problem then someone might be able to suggest you a better way to do it. Maybe there is a solution that does not require an index
In Section
Seekers of Perl Wisdom
|
|