Date: 3 Jul 2001 19:56:01 GMT
From: Jim Monty <monty@primenet.com>
Subject: Re: Finding the subscript of an item in an array...
Message-Id: <9ht80h$lgp$1@nnrp2.phx.gblx.net>

Weston Cann <iowa_song88.remove_eights_and_this@hotmail.com> wrote:
> I know that you can find the subscript of an item in an array by
> doing something like this:
>
> my @foo = ('Travis','Bonnie','Flipper','Arnold','Deluxe');
> my $item = 'Flipper';
>
> my $index = 0;
> my $subscript;
>
> while($index <= $#foo)
> {
>    if($item eq $foo[$index])
>    { $subscript = $index; }
>
>    $index++;
> }
>
> This is OK, but it's sortof bulky. I'm wondering if there's a neat-o
> perl idiom/single line kind of way to do the same thing.

    foreach my $index (0 .. $#foo) {
        $subscript = $index, last if $item eq $foo[$index];
    }

    for (0 .. $#foo) {
        $subscript = $_, last if $item eq $foo[$_];
    }

-- 
Jim Monty
monty@primenet.com
Tempe, Arizona USA


