On Wed, May 02, 2001 at 11:10:22AM +0100, Michael G Schwern wrote:
> On Wed, May 02, 2001 at 11:13:13AM +0200, Alexander Farber (EED) wrote:
> > I would like to propose adding the "last" statement
> > to the "grep", which currently doesn't work:
> 
> For the record, I have no problem with this. :)
> 
> 
> >   maas34: perl -e 'grep { print and $_ == 3 and last } (1,2,3,4,5)'
> >   123
> >   Can't "last" outside a loop block at -e line 1.
> > 
> > This way it would be possible to use such constructs:
> 
> FYI, its not hard to write a routine to do just that...
> 
>         first { some_condition } @stuff;

See List::Util in perl-current

=item first BLOCK LIST

Similar to C<grep> in that it evaluates BLOCK setting C<$_> to each element
of LIST in turn. C<first> returns the first element where the result from
BLOCK is a true value. If BLOCK never returns true or LIST was empty then
C<undef> is returned.

    $foo = first { defined($_) } @list    # first defined value in @list
    $foo = first { $_ > $value } @list    # first value in @list which
                                          # is greater than $value


Graham.

Reply via email to