On Thu, 2005-01-27 at 13:10, Mallik wrote:
Hi,
Thanks for ur reply.
Is there any function instead of looping thru entire array.
Mallik.
You can do fancy stuff like below, but this may not be necessarily
better. Can you post where you are trying to use this ?
What is there is more
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.
for my $i ( 0 .. $#arr ) {
if ( $arr[ $i ] eq 'pqr' ) {
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
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
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;
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
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
Another problem occurs if the keys are not unique, depending on whether
you want the first, last or all occurrences of the string.
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
You can use regular expression
Eh? You mean a regular construct?
alfred,
[EMAIL PROTECTED] wrote:
Hi Mallik
You can use regular expression
-
$i=0;
foreach $temp (@arr)
{
if ( $temp eq 'pqr' )
{
print index is == $i \n;
last;
}
$i++;
}
Hi Mallik
You can use regular expression
-
$i=0;
foreach $temp (@arr)
{
if ( $temp eq 'pqr' )
{
print index is == $i \n;
last;
}
$i++;
}
-
I hope this will work ,but ur desired index is 3 not 4.
Mallik ..
Deserve before
-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',
Hi,
Thanks for ur reply.
Is there any function instead of looping thru entire array.
Mallik.
-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: Thursday, January 27, 2005 1:06 PM
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
Cc: beginners@perl.org
Subject: RE: How
12 matches
Mail list logo