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:
This doesn't work, because of the (documented in perlop) magic comparison with $. when the conditions are constants:# 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
Ok, fair enough. But this doesn't work correctly, and there aren't any warnings:# 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.
In fact, as flip-flops return the sequence numbers of their matches, we can double check what these are:# 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
Eh? Why does it start back at 1 after 4E0?# 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
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 | |
by wu-lee (Beadle) on May 22, 2009 at 11:05 UTC | |
by ikegami (Patriarch) on May 22, 2009 at 15:11 UTC | |
by Roy Johnson (Monsignor) on May 26, 2009 at 16:22 UTC | |
Re: Flip-flop won't DWIM
by akho (Hermit) on May 21, 2009 at 15:49 UTC | |
by Roy Johnson (Monsignor) on May 21, 2009 at 20:30 UTC | |
by wu-lee (Beadle) on May 22, 2009 at 10:54 UTC | |
by akho (Hermit) on May 21, 2009 at 15:57 UTC | |
by wu-lee (Beadle) on May 21, 2009 at 16:03 UTC | |
by wu-lee (Beadle) on May 21, 2009 at 15:58 UTC | |
by akho (Hermit) on May 21, 2009 at 16:15 UTC | |
by wu-lee (Beadle) on May 21, 2009 at 16:13 UTC | |
by ikegami (Patriarch) on May 21, 2009 at 16:57 UTC |
Back to
Seekers of Perl Wisdom