You better use hashes for data structures, but if not the case or you need 
to find that in an array, you can set a very simple sub in your code to do 
something like this:


use strict;
use warnings;

my @list = qw(uno dos tres cuatro cinco);
my $index = inArray(@list, 'tres');

sub inArray {
    my $s = pop @_;
    for (0..$#_) {
        return $_ if $_[$_] eq $s;
    }
    return -1; # -1 means it's not in array
}

HTH

Paco Zarabozo




----- Original Message ----- 
From: [EMAIL PROTECTED]
To: [email protected]
Sent: Monday, October 22, 2007 3:13 PM
Subject: Index of element in array?



Gurus,

I'm unable to find this in the Camel, Panther, or Billygoat books, so I'll 
ask you... Is there a Perl function that returns the index of a given 
element in an array?  For example:

my @list = q( apple  banana  pear  grapefruit);

my $look4 = "banana";

my $ndx = somefunc( $look4, @list ); # sets $ndx to 1

$look4 = "monkeywrench";

$ndx = somefunc( $look4, @list); # sets $ndx to -1, or maybe undef?

I'm trying to find a more elegant way than running a foreach loop over 
@list.

Thanks!

Deane Rothenmaier
Programmer/Analyst
Walgreens Corp.
847-914-5150

A word to the wise ain't necessary, it's the stupid ones who need the 
advice. -- Bill Cosby



_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs 

_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to