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.)


In reply to Flip-flop won't DWIM by wu-lee

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.