Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Flip-flop won't DWIM

by wu-lee (Beadle)
on May 21, 2009 at 15:39 UTC ( #765480=perlquestion: print w/replies, xml ) Need Help??

wu-lee has asked for the wisdom of the Perl Monks concerning the following question:

I want to use a flip-flop to truncate a list after a certain value. This should be easy, right? After all, getting a sub-sequence works:
# Get everything between B and D: perl -lwe 'print join " ", grep scalar(/B/../D/), qw(A B C D E F)' # prints: # B C D
This doesn't work, because of the (documented in perlop) magic comparison with $. when the conditions are constants:
# Get everything between up to D: perl -lwe 'print join " ", grep scalar(1../D/), qw(A B C D E F)' # prints: # Use of uninitialized value in range (or flip) at -e line 1. # Use of uninitialized value in range (or flip) at -e line 1. # Use of uninitialized value in range (or flip) at -e line 1. # Use of uninitialized value in range (or flip) at -e line 1. # Use of uninitialized value in range (or flip) at -e line 1. # Use of uninitialized value in range (or flip) at -e line 1.
Ok, fair enough. But this doesn't work correctly, and there aren't any warnings:
# Get everything up to and D: perl -lwe 'print join " ", grep scalar(/./../D/), qw(A B C D E F)' # prints: # A B C D E F
In fact, as flip-flops return the sequence numbers of their matches, we can double check what these are:
# Get everything up to D: perl -lwe 'print join " ", map scalar(/./../D/), qw(A B C D E F)' # prints: # 1 2 3 4E0 1 2
Eh? Why does it start back at 1 after 4E0?

Can anyone shed any light on this? (I'm using Perl 5.8.8 on Ubuntu.)

Replies are listed 'Best First'.
Re: Flip-flop won't DWIM
by ikegami (Patriarch) on May 21, 2009 at 16:52 UTC

    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/ }'
      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
Re: Flip-flop won't DWIM
by akho (Hermit) on May 21, 2009 at 15:49 UTC
    The /./ matches E, so flip-flop starts over.
      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!
        Yes. However, List::MoreUtils is not in core, so it's not something I want to rely on in the current circumstance.
      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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://765480]
Approved by jettero
Front-paged by wfsp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2023-05-28 02:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?