On Wed, Dec 04, 2002 at 08:08:48PM -0700, Luke Palmer wrote:
> About your idea, though, I'm rather indifferent.  However, a friend of
> mine once asked me if Perl had "search" or "find" operation, returning
> the I<index> of matching elements.  Now am I just being braindead, or
> is Perl actually missing this operation?  Do you really have to:
> 
>     my $index;
>     for @a {
>         last if la_dee_daa;
>         $index++;
>     }
> 
> That can't be right....

You can do it with a map without much trouble:

    my @indexes = map { /condition/ ? $i++ : () } @stuff;

Its so rare that you work by array index in Perl.  Usually things flow
together element by element.  Sort of like how you rarely handle strings
character by character which can severely confuse C programmers.

What was your friend trying to do?


-- 

Michael G. Schwern   <[EMAIL PROTECTED]>    http://www.pobox.com/~schwern/
Perl Quality Assurance      <[EMAIL PROTECTED]>         Kwalitee Is Job One
Stupid am I?  Stupid like a fox!

Reply via email to