Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Flip-flop won't DWIM

by ikegami (Patriarch)
on May 21, 2009 at 16:52 UTC ( [id://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:

Fix:

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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 04:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found