Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Flip-flop won't DWIM

by akho (Hermit)
on May 21, 2009 at 15:49 UTC ( #765484=note: print w/ replies, xml ) Need Help??


in reply to Flip-flop won't DWIM

The /./ matches E, so flip-flop starts over.


Comment on Re: Flip-flop won't DWIM
Replies are listed 'Best First'.
Re^2: Flip-flop won't DWIM
by Roy Johnson (Monsignor) on May 21, 2009 at 20:30 UTC
    So use the match-once operator (no need for an actual pattern; the empty pattern works fine):
    print join ' ', grep scalar(??../D/), ('A'..'F');

    Caution: Contents may have been coded under pressure.
      Aha! Finally, a use for the match-once operator! And it seems to be tailor made for this problem. Bravo!
Re^2: Flip-flop won't DWIM
by akho (Hermit) on May 21, 2009 at 15:57 UTC
      Yes. However, List::MoreUtils is not in core, so it's not something I want to rely on in the current circumstance.
Re^2: Flip-flop won't DWIM
by wu-lee (Beadle) on May 21, 2009 at 15:58 UTC
    Right. Of course, silly me.

    Ok, so this does what I want:

    perl -lwe '$x = 1; print join " ", grep { scalar($x++../D/) } qw(A B C + D E F)' # prints: # A B C D
    Although this is not particularly neat. Is there a better way?
      Your code prints "A B C D E F" for me, and I cannot see why it would print anything else.

      This seems to work if you insist on using flip-flop:

      $x = 1; print join " ", grep { $x..(($x=0) or /D/) } qw(A B C D E F);
      However, you could copy stuff from List::MoreUtils.
      Actually, that doesn't, but this does:
      perl -lwe '$x = 0; print join " ", grep { scalar(!$x++../D/) } qw(A B + C D E F)' # prints: # A B C D
      The "++" does nothing.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2015-07-30 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls