I think the problem is that when the ".." evaluates the left hand side and it becomes true, it will remain true until the right hand side becomes true. So once you are in range for, say line "A" becuase of STMT1=BEGIN, it will still be in range when you test /STMT2=BEGIN/../STMT2=END/ since it hasn't seen /STMT2=END/ yet. The following works ok, but not sure it's exactly what you want (BTW, "use strict" might help.)
while ( defined ($_ = <DATA>) ) { if (/RULE=$rule/../^END$/) { chomp $_; if ($_ =~ /^NUMOFRULES/) { # Get the number of rules @rulenos = split(/=/); } my $e = $rulenos[1]; if (/^STMT1=BEGIN$/../^STMT$e=END$/) { if ( !($_ =~/^STMT/)) { print $_; } } # for ($i = 1; $i <= $rulenos[1]; $i++) { # if (/^STMT$i=BEGIN$/../^STMT$i=END$/) { # if ( !($_ =~/^STMT/)) { # print $_; # } # } # } } } __DATA__ RULE=test BEGIN NUMOFRULES=2 STMT1=BEGIN A B C STMT1=END STMT2=BEGIN X Y Z STMT2=END
Update. a subtle point which I didn't know until thinking about this problem is that: each .. keeps its own state of in range or not in range, here "each" is NOT dependent on what the left-right operators are, so each iteration of a loop only counts as one range operator, and one state, as shown by the following code:
use strict; my @data = <DATA>; foreach(@data){ chomp; if(/START1/../END1/){ print "1: $_\n"; } if(/START2/../END2/){ print "2:$_\n"; } } print "\n\n"; foreach (@data){ foreach my $i(1..2){ if(/START$i/../END$i/){ print "$i:$_\n"; } } } __DATA__ START1 A B C END1 START2 X Y Z END2
output is as follows, notice the difference:
__OUTPUT__ 1: START1 1: A 1: B 1: C 1: END1 2:START2 2:X 2:Y 2:Z 2:END2 1:START1 2:START1 1:A 2:A 1:B 2:B 1:C 2:C 1:END1 2:START2 1:X 2:X 1:Y 2:Y 1:Z 2:Z 1:END2 2:END2

In reply to Re: Problem in using range operator by johnnywang
in thread Problem in using range operator by tariqahsan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.