in reply to Re: flip-flop flop
in thread flip-flop flop

Suffers from the same problem the OP describes. If you would use that grep a second time (because it's in a sub, or in a loop), the $z doesn't get reset -- that's what state is doing.

Better (but untested):

my @y = do {my $z = 0; grep {$z ||= $_ == 3} @x};
Note that doesn't evaluate the condition ($_ == 3) after finding a match.

Replies are listed 'Best First'.
Re^3: flip-flop flop
by kcott (Archbishop) on Mar 30, 2012 at 11:01 UTC

    ++ Thanks JavaFan, I've updated my post.

    -- Ken