develooper Front page | perl.perl6.language | Postings from May 2001

Re: Please make "last" work in "grep"

Thread Previous | Thread Next
From:
Michael G Schwern
Date:
May 2, 2001 03:10
Subject:
Re: Please make "last" work in "grep"
Message ID:
20010502111022.A549@blackrider.blackstar.co.uk
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   <schwern@pobox.com>    http://www.pobox.com/~schwern/
Perl6 Quality Assurance     <perl-qa@perl.org>	     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

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About