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.)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.