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