"Steve Grazzini" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > On Wed, Jul 16, 2003 at 06:50:21PM +0200, Kevin Pfeiffer wrote: > > I found this in my "to do" directory (was this from perl.beginners - I don't > > remember and can't find it via Google). I tried the "line-range" operator > > here, but am wondering if it's possible with it to *not* catch the matching > > lines (i.e. only lines "3"-"4"). I suppose if you captured into an array > > then you could just shift and unshift. I also tried a second way below not > > using range that seems to work well (or?). > > There's nothing wrong with the trick you used (and it might be > easier to understand than mine) but you can also switch on the > range operator's return value: > > while (<DATA>) { > my $range = /line2/../line5/; > print if $range > 1 # skip the first line > and $range !~ /E/; # and the last (which looks like "4E0") > } > > I don't think this is documented (is it?) but it can be handy.
No, I don't think it is documented (at least not on 5.6) but it's clear you're meant to use it this way; this is one of the dark secrets of Perl. I believe that perldoc perlop (search for 'Range Operators') should read something like Each ".." operator maintains its own boolean state. It [returns a sequence number starting at one] until the right operand is true, *AFTER* which the range operator becomes false again. With the adjunct that the last /true/ value of the operator will be expressed in engineering format, witness the program below whichshows that the first line and the last line can be tested by checking for a value of one and a regex match of /E/ respectively. Together with the difference between the .. and the ... operator, I shall pause before I dig further... HTH somebody :) Rob while (<DATA>) { my $xx = /line2/ .. /line5/; print defined $xx ? "'$xx'" : 'undef', "\n"; } __DATA__ This is line1 some text on line2 line3 all about me good stuff on line4 line5 isn't that great I'm on line6 OUTPUT '' '' '1' '2' '3' '4E0' -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]