-----Original Message-----
From:   Mallik [mailto:[EMAIL PROTECTED]
Sent:   Thu 1/27/2005 12:56 PM
To:     Perl-Trolls
Cc:     beginners@perl.org
Subject:        How to find the Index of an element in array?
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>

The index in your case is 3 not 4.
You can achieve by doing -

for(my $i = 0; $i < @arr; $i++)
{
 if($arr[$i] eq 'pqr')
 {
  print "index is $i\n";
 }
}


There may be better ways. I would also like to know if there is any better and 
cleaner way.
Thanks





--
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