Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Flip-flop won't DWIM

by ikegami (Patriarch)
on May 21, 2009 at 16:52 UTC ( #765503=note: print w/replies, xml ) Need Help??

in reply to Flip-flop won't DWIM

This doesn't work, because of the (documented in perlop) magic comparison with $. when the conditions are constants:


perl -lwe'$true=1; print for grep $true../D/, qw(A B C D E F)'

Note that it will flip for "E" (like your last version). What you probably intended is:

perl -lwe'my @a=qw(A B C D E F); print for grep ($_==1) .. ($a[$_]=~/D +/), 0..$#a'

Or without a flip-flop:

perl -lwe'for (qw( A B C D E F )) { print; last if /D/ }'

Replies are listed 'Best First'.
Re^2: Flip-flop won't DWIM
by wu-lee (Beadle) on May 22, 2009 at 11:05 UTC
    Thanks - in the end I also decided a flip-flop wasn't really necessary and used a more pedestrian loop. This post was mainly to see if there was in fact a nice way of using a flip flop in this case; I think ??../ D/ fits the bill.
      Not really
      >perl -le"for (1..2) { print for grep scalar(??../D/), ('A'..'F'); }" A B C D
        An opportunity for the seldom-used reset function to come into play:
        perl -le"for (1..2) { print for grep scalar(??../D/), ('A'..'F'); rese +t}"

        Caution: Contents may have been coded under pressure.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://765503]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2023-10-03 14:40 GMT
Find Nodes?
    Voting Booth?

    No recent polls found