Index of element in array?

Index of element in array?

am 22.10.2007 22:13:21 von Deane.Rothenmaier

This is a multipart message in MIME format.
--===============1709475933==
Content-Type: multipart/alternative;
boundary="=_alternative 006F1FA18625737C_="

This is a multipart message in MIME format.
--=_alternative 006F1FA18625737C_=
Content-Type: text/plain; charset="US-ASCII"

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
--=_alternative 006F1FA18625737C_=
Content-Type: text/html; charset="US-ASCII"



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

--=_alternative 006F1FA18625737C_=--


--===============1709475933==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1709475933==--

RE: Index of element in array?

am 22.10.2007 22:22:18 von martin.thurn

use List::MoreUtils qw( first_index );
my $i = first_index { $_ eq 'banana' } @list;

HOWEVER, you should consider whether you really want to use indexes into
lists. In general it is preferred just to iterate over items in a list
directly. (See "Perl Best Practices" book from O'Reilly)

P.S. Please send only PLAIN TEXT email to mailing lists.

- - Martin



________________________________

From: activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of
Deane.Rothenmaier@walgreens.com
Sent: Monday, October 22, 2007 16:13
To: activeperl@listserv.ActiveState.com
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);

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Index of element in array?

am 22.10.2007 22:46:44 von Andy_Bach

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

Usually, looking for the index is a sign of a data structure issue. Arrays
are, by nature, not data addressable, that's what hashes are for. A trick
would be to transform it into a hash (w/ the index as the value) and look
that way but that's only elegant in the eye of certain transformer folks.
You can *just* determine if it's in there by grep:
my $in_there = grep { /^banana$/ } @list;

Or you could, if you hadn't tricked us:
my @list = q( apple banana pear grapefruit);

returns a single string i.e
$list[0] eq ' apple banana pear grapfruit';

You want "quote word" ;->
my @list = qw( apple banana pear grapefruit);

anyway. You can turn it into a hash:
my $in_there = grep { /^$look4$/ } @list;
my $cnt = 0;
my %at_where = map { $_ => $cnt++; } @list;
print "Yep $at_where{$look4}\n" if $in_there;

You can try a map in a void context:
my $at_where = 0;
my $cnt = 0;
map { $cnt++; $at_where = $cnt -1 if /^$look4$/ ; } @list;

but, as LW says "I wouldn't want to map anybody in a void context" ... or
words to that effect.

a

Andy Bach
Systems Mangler
Internet: andy_bach@wiwb.uscourts.gov
VOICE: (608) 261-5738 FAX 264-5932

Wire telegraph is a kind of a very, very long cat. You pull his tail in
New York and his head is meowing in Los Angeles. And radio operates
exactly the same way. The only difference is that there is no cat.
--Albert Einstein (explaining radio)
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Index of element in array?

am 22.10.2007 23:25:41 von fzarabozo

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: Deane.Rothenmaier@walgreens.com
To: activeperl@listserv.ActiveState.com
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
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Index of element in array?

am 22.10.2007 23:44:22 von Wayne Simmons

Look at the grep command in Perl, you can specify a block of code to be
applied to each value in the list, perhaps it will help. However, heed the
advice of others if you can. I understand that sometimes you=92re give garb=
age
to work with and best practices go out the window.

Also note that a foreach loop doesn=92t have a guaranteed order.

-Wayne Simmons

--
Software Engineer
InterSystems USA, Inc.
303-858-1000
=A0 =

________________________________________
From: activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of
Deane.Rothenmaier@walgreens.com
Sent: Monday, October 22, 2007 2:13 PM
To: activeperl@listserv.ActiveState.com
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? =A0For example: =


my @list =3D q( apple =A0banana =A0pear =A0grapefruit); =


my $look4 =3D "banana"; =


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


$look4 =3D "monkeywrench"; =


$ndx =3D 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
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Index of element in array?

am 23.10.2007 00:07:31 von fzarabozo

There's a guaranteed order for arrays. There's no order for hashes (unless
you give a sortable name to your keys).

HTH

Paco Zarabozo


> ----- Original Message -----
> From: Wayne Simmons
>
> Also note that a foreach loop doesn't have a guaranteed order.
>
> Wayne Simmons

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Index of element in array?

am 23.10.2007 00:18:01 von Andy_Bach

> Also note that a foreach loop doesn?t have a guaranteed order.

Er, for an array, a foreach loop will go from front to back (or back to
front if you 'reverse' it). Hashes or hash keys will come out in a
non-determinable (or close enough) order but arrays darn well better come
out in order or something's wrong.
my @list = qw( apple banana pear grapefruit);
print "Will be: ", join(", ", @list), "\n";
foreach my $fruit ( @list ) {
print "$fruit\n";
}
my %at_where = map { $_ => $cnt++; } @list;
print "Probably won't be: ", join(", ", @list), "\n";
foreach my $key ( keys %at_where ) {
print "$key ($at_where{$key})\n";
}

Will be: apple, banana, pear, grapefruit
apple
banana
pear
grapefruit
Probably won't be: apple, banana, pear, grapefruit
grapefruit (3)
banana (1)
apple (0)
pear (2)

The hash may come out consistently, run to run, in that order but it won't
be the order you put them into the hash ... probably. And adding a new
element will probably change the order (added 'kiwi' to the array):
Will be: apple, banana, pear, grapefruit, kiwi
apple
banana
pear
grapefruit
kiwi
Probably won't be: apple, banana, pear, grapefruit, kiwi
grapefruit (3)
banana (1)
apple (0)
kiwi (4)
pear (2)

a

Andy Bach
Systems Mangler
Internet: andy_bach@wiwb.uscourts.gov
VOICE: (608) 261-5738 FAX 264-5932

Wire telegraph is a kind of a very, very long cat. You pull his tail in
New York and his head is meowing in Los Angeles. And radio operates
exactly the same way. The only difference is that there is no cat.
--Albert Einstein (explaining radio)
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Index of element in array?

am 23.10.2007 10:18:35 von Brian Raven

From: activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of
Deane.Rothenmaier@walgreens.com
Sent: 22 October 2007 21:13
To: activeperl@listserv.ActiveState.com
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 =3D q( apple banana pear grapefruit); =

> =

> my $look4 =3D "banana"; =

> =

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

> =

> $look4 =3D "monkeywrench"; =

> =

> $ndx =3D 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. =


You have already received some good answers (particularly Martin's
suggestion of List::MoreUtils), but I shall ask, what seems to me, the
obvious question. What are you trying to achieve by doing this, as there
may be a more 'perl-ish' way of doing it than finding the index of
elements in an array. I believe that the main reason that such a
function does not already exist in core Perl is that a) it is very
rarely necessary, and b) it is fairly trivial to implement when it is
(see the code for List::MoreUtils).

If you can answer this, then you might get an even more useful
suggestion.

HTH

Also, can I echo Martin's request to post in plain text.

-- =

Brian Raven =


==================== =====3D=
================
Atos Euronext Market Solutions Disclaimer
==================== =====3D=
================

The information contained in this e-mail is confidential and solely for the=
intended addressee(s). Unauthorised reproduction, disclosure, modification=
, and/or distribution of this email may be unlawful.
If you have received this email in error, please notify the sender immediat=
ely and delete it from your system. The views expressed in this message do =
not necessarily reflect those of Atos Euronext Market Solutions.

Atos Euronext Market Solutions Limited - Registered in England & Wales with=
registration no. 3962327. Registered office address at 25 Bank Street Lon=
don E14 5NQ United Kingdom. =

Atos Euronext Market Solutions SAS - Registered in France with registration=
no. 425 100 294. Registered office address at 6/8 Boulevard Haussmann 750=
09 Paris France.

L'information contenue dans cet e-mail est confidentielle et uniquement des=
tinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. Tou=
te copie, publication ou diffusion de cet email est interdite. Si cet e-mai=
l vous parvient par erreur, nous vous prions de bien vouloir prevenir l'exp=
editeur immediatement et d'effacer le e-mail et annexes jointes de votre sy=
steme. Le contenu de ce message electronique ne represente pas necessaireme=
nt la position ou le point de vue d'Atos Euronext Market Solutions.
Atos Euronext Market Solutions Limited Soci=E9t=E9 de droit anglais, enregi=
str=E9e au Royaume Uni sous le num=E9ro 3962327, dont le si=E8ge social se =
situe 25 Bank Street E14 5NQ Londres Royaume Uni.

Atos Euronext Market Solutions SAS, soci=E9t=E9 par actions simplifi=E9e, e=
nregistr=E9 au registre dui commerce et des soci=E9t=E9s sous le num=E9ro 4=
25 100 294 RCS Paris et dont le si=E8ge social se situe 6/8 Boulevard Hauss=
mann 75009 Paris France.
==================== =====3D=
================

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs