in reply to Finding part of a file

Expanding on blakem's suggestion, here's what I'd do:

#!/usr/bin/perl -w use strict; my ($start, $end) = qw(banana grape); while (<DATA>) { if (/^$start$/ .. /^$end$/) { print unless /^($start|$end)$/; } } __END__ apple banana pear peach grape orange

Update: Thanks to blakem for pointing out the missing $ on the end of the flip-flop. He also points out that this may be a good place to use the /o option to the match operator and I agree. You should probably benchmark to see how much of a gain you get.

--
<http://www.dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re: Re: Finding part of a file
by runrig (Abbot) on Dec 05, 2001 at 03:27 UTC
    I don't know if this is any better, but just for variety (using an extra variable, but shorter match pattern):
    if (my $status = /^$start$/ .. /^$end$/) { print unless $status =~ /^1$|E/; } # Or no extra variable, but getting more obfuscated :) print if (/^$start$/../^$end$/) !~ /^1?$|E/;
      Nifty... I didn't know that the flip flop returned anything other than 1 or 0.
      % perl -lne 'printf "%-6s %s\n", $_, scalar(/^banana$/ .. /^grape$/) +' fruit.txt apple banana 1 pear 2 peach 3 grape 4E0 orange
      So, I can tweak my one-liner to exclude the endpoints like so:
      % perl -ne 'print unless (/^banana$/ .. /^grape$/) =~ /^1?$|E/' fruit +.txt pear peach
      Update:
      Doh!, runrigs second stanza was added while I was replying.... I guess obfuscated minds think alike. ;-)

      -Blake