my @array = ( ">blue" "sky" "skirt" "sea" ">white" "paper" "flower" ">red" "face" "flower" ">green" "grand" "tree" )
Say, I need get all elements after ">white" before ">red". Be notice, any new color begin with ">", so, I think I can
my $element;
foreach ($element(@array)){
if ($element =~ /white/){
# find the element contains word "white" and print it;
print "$element"; my $index = /numeric index of $elements/;
my $n = 1;
if ($array[$index+$n] =~ /!^>/){
# get the nex element by numeric index and check whether it begins with ">", if not, print it;
print "$array[$index+$n]"; $n +=1;
}else {
# if the element begins with ">", end the loop
last;
}
}
}
But I do not how to get the numeric index of $elemnet. Or is there any other way to jump into next element?
any suggestions are appreciated!
Frank
Octavian Rasnita wrote:
Hi,
Let's say you have the following array:
my @array = ( 'blue', 'white', 'red', 'blue', 'green', 'blue', );
And let's say you want to get the index for the 'blue' element.
I guess it is obvious that you need to analyse element by element, in order to be able to find if that array contains dupplicates and if you will find dupplicates, you might need to return a list of indexes.
foreach(@array) { if ($_ eq 'blue') { #do something } }
Teddy
----- Original Message ----- From: "Frank" <[EMAIL PROTECTED]>
To: "Ing. Branislav Gerzo" <[EMAIL PROTECTED]>
Cc: <beginners@perl.org>
Sent: Sunday, May 15, 2005 6:34 PM
Subject: Re: How to get the numeric index of a element in an array
Ing. Branislav Gerzo wrote:
Frank [F], on Sunday, May 15, 2005 at 19:45 (+0800) typed:
F> If i know the element of array, can I get the numeric index of this F> element?
Exist a way, but it is better using hash. In arrays you have to iterate over every element.
~~~~~~~Pls tell me the way. possibly I need use it. I need get the elements from an array after some specific element. Say, I find a elements contains the word I want by regular expression ($_ =~ /word1/), and then I need get the following element untill another words appears ($_ =~ /word2/). I thought the best way for me is to get the numeric index of the elements and get the elements i want. Do you have any suggestions?
Thanks,
Frank
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>