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;

sub first (&@) {
    my($cond, @stuff) = @_;
    my $first;
    foreach (@stuff) {
        if( &$cond ) {
            $first = $_;
            last;
        }
    }

    return $first;
}

print first { $_ > 5 } 1..10;
6

And I'm quite sure there's an RFC for doing something like this alread
up for Perl 6.


> Here I am looking for a button with a special name - "Delete ..." -
> and there can be only one such button, so I have to interrupt the
> "grep" after I find it (otherwise the $1, $2, $3, $4 might be unset).

(grep {...} @stuff)[0] will work, but its inelegant.


-- 

Michael G. Schwern   <[EMAIL PROTECTED]>    http://www.pobox.com/~schwern/
Perl6 Quality Assurance     <[EMAIL PROTECTED]>       Kwalitee Is Job One
Maybe they hooked you up with one of those ass-making magazines.
        -- brian d. foy as misheard by Michael G Schwern

Reply via email to