Nah makes perfect sense !! ;)

Grep Power(tm) 

-----Original Message-----
From: Ankur Gupta [mailto:[EMAIL PROTECTED] 
Sent: 27 January 2005 13:15
To: Manav Mathur
Cc: Rob Napier (rnapier); Richard Chycoski (rac); Mallikarjun Kodicherla
(mkodiche); perl-trolls (mailer list); beginners@perl.org
Subject: Re: How to find the Index of an element in array?

I have another solution using grep but its kinda weird....

@array = (..................); #your array here $i=1; eval{ grep {
(/$element/o && die ) or ++$i } @array; }; print "index of $element =
$i\n"; #prints the index of $element.. If $i  > scalar(@array), element
not found.

--
Ankur

Manav Mathur wrote:

>Rob, In your solution
>$hash{'pqr'} will return 3. 
>- iniitalize $i with 1
>or
>-
>%hash=map{$_=>[EMAIL PROTECTED];
>
>Manav
>
>|-----Original Message-----
>|From: Rob Napier [mailto:[EMAIL PROTECTED]
>|Sent: Thursday, January 27, 2005 2:06 PM
>|To: Richard Chycoski
>|Cc: Mallik; Perl-Trolls; beginners@perl.org
>|Subject: Re: How to find the Index of an element in array?
>|
>|
>|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>
>|
>|
>
>*********************************************************
>Disclaimer:          
>
>This message (including any attachments) contains confidential 
>information intended for a specific individual and purpose, and is 
>protected by law.
>If you are not the intended recipient, you should delete this message 
>and are hereby notified that any disclosure, copying, or distribution 
>of this message, or the taking of any action based on it, is strictly 
>prohibited.
>
>*********************************************************
>Visit us at http://www.mahindrabt.com
>
>
>  
>

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to