A linear search can find all occurrences, and will be faster for single lookup, but using a hash will consume less CPU (at the expense of memory) for large numbers of lookups. If there is a possibility of multiple occurrences of the same key, it gets more complicated, since loading the hash will wipe out earlier references with later ones unless you use a hash of arrays. I didn't use the 'map' function to build the hash in my example because I wasn't assuming that the data was already in an array, so I showed the simple initialisation case. Loading the hash might also be part of your data validation process - and you can use the hash to detect duplicate keys, e.g., you can do a check for 'defined( $hash{ $key })' and generate an error message if you don't want multiple occurrences of the key in your data.
A lot of it really depends on your situation - I assumed that the example was a reduction of the problem to its simplest form.
The really good thing about Perl is that there's usually N ways to solve a given problem, where N is the number of Perl programmers in the room.
The really bad thing about Perl is that there's usually N ways to solve a given problem, where N is the number of Perl programmers in the room.
(:-)
- Richard
Rob Napier wrote:
In terms of time efficiency, if you only need one lookup, the linear walk is almost certainly faster, since you don't have to hash the entire list. You can stop as soon as you find the answer (which you expect to be half-way down the list). But the speed difference is almost certainly negligable. In terms of space efficiency the array is much smaller too which matters if the hash gets particularly large (large enough that the speed difference isn't negligable).
I agree with "depending on what else you need to do with your data." If a list is more natural for the data, definitely leave it in a list and just walk it. If you need to do a lot of lookups (or other hash-like things), you can hash the list as Richard suggests pretty easily:
my $i = 0; my %hash = map { $_ => $i++ } @list; print $hash{$search};
And of course the above is short and simple, so maybe that's nice even if you're only doing it once and don't mind being slower and bigger (memorywise).
If you want to stay with a fast linear search, there have been a couple of good suggestions. Tapasranjan's version is close, but does too much searching. You should do a 'last' after you've found the data. Arjun's solution is good, or you can do this kind of shortened form:
my $search = 'pqr'; for(my $i = 0; $i < @arr; $i++) { ($arr[$i] eq $search) && do { print "index is == $i\n"; last } }
Note that in this form, $i is not valid outside the loop. You'll need to move the "my $i" outside the for() loop if you need it later in the code.
-RobN
Richard Chycoski wrote:
You would have to 'walk' the whole array, which is rather inefficient. However, depending on what else you need to do with your data, a hash could do better than an array:
%myhash = ('abc'=>1, 'xyz'=>2, 'mno'=>3, 'pqr'=>4, 'stu'=>5, 'sdfd'=>6);
and then $myhash{ 'pqr') wiil return 4.
- Richard
Mallik wrote:
I need to find the index of a particular element in array. For eg., @arr = ('abc', 'xyz', 'mno','pqr','stu','sdfd');
I want to find the index of the element 'pqr' which is 4.
Any function/code to achieve this.
Thanks in advance,
Mallik.
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>