CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to know an element's index in the array??help!

 



tack
Novice

Feb 11, 2003, 5:46 PM

Post #1 of 4 (221 views)
How to know an element's index in the array??help! Can't Post

eg.

@array=(1,2,3,4)

$a=4

So the index of $a is 5. Is there a easy function to get it ?

thanks very much.


davorg
Thaumaturge / Moderator

Feb 12, 2003, 2:01 AM

Post #2 of 4 (216 views)
Re: [tack] How to know an element's index in the array??help! [In reply to] Can't Post

Not sure what you mean. That array doesn't have an index of 5. Please explain further.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Paul
Enthusiast

Feb 12, 2003, 2:36 AM

Post #3 of 4 (214 views)
Re: [tack] How to know an element's index in the array??help! [In reply to] Can't Post

The index is 3.

Look here for your answer:

http://www.perlmonks.com/index.pl?node_id=113951


tack
Novice

Feb 18, 2003, 6:53 AM

Post #4 of 4 (202 views)
Re: [davorg] How to know an element's index in the array??help! [In reply to] Can't Post

i think i must say sorry at first . Yes! i made an mistake .The index of $a in the array is 3.

now , i have made a function to get it . Here:


Code
   

sub ioa
{
my ($b,@a)=@_;
$n=-1;
foreach $c (@a) {
$n++;
if ($b eq $c) {
last;
}
}
return($n);
}



So my foregoing question can be solved like this:


Code
   

$index=ioa($a ,@array);



thank you for your attention to my problem.


(This post was edited by tack on Feb 18, 2003, 7:05 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives