in reply to Re: Matching ranges
in thread Matching ranges

This is the weirdest parsing problem I have ever encountered

while(<>) { if (my $r = /^Services/ .. /^Users/ ) { next if $_ =~ /^Services/; print $_; last if $r =~ /E0$/; } }

produces output without the "Services" line, but the following does not (at least for me):

while(<>) { if (my $r = /^Services/ .. /^Users/ ) { next if $r =~ /^Services/; print $_; last if $r =~ /E0$/; } }

even when I try to exit early, $r is not seemingly not respected

while(<>) { if (my $r = /^Services/ .. /^Users/ ) { #next if $_ =~ /^Services/; exit if $r =~ /^Services/; print $_; last if $r =~ /E0$/; } }

Replies are listed 'Best First'.
Re^3: Matching ranges
by Marshall (Canon) on Sep 02, 2010 at 14:24 UTC
    A slight bit more "regex kung-fu" is needed.

    #!/usr/bin/perl -w use strict; my $num =1; while(<DATA>) { if ( (/^Services\s*$/../^Users\s*$/) =~ /^(\d+)(?<!^1)$/ ) { last if $1 <= $num++; #only first Services section next if /^====/ || /^\s*$/; #if you don't want these print "$_"; } } =prints blah glah sfsd asfsdf afsafdf =cut
    Data Used: I highly recommend reading: not only Flipin good, or a total flop?, but some of the responses in that node that talk about eliminating start and end conditions - shows how to do "regex-fu" like above!

      Sir, you are a true PerlMonk, with special kung-fu-regex skills. Thank you much!

Re^3: Matching ranges
by Marshall (Canon) on Sep 02, 2010 at 14:34 UTC
    Your $r =~ /^Services/ is the problem. Try running this and look at printout of values of $r.
    while(<>) { if (my $r = /^Services/ .. /^Users/ ) { #next if $r =~ /^Services/; next if /^Services/; print "$r:$_"; last if $r =~ /E0$/; } }

      Most excellent ... Seriously thanks ...