There are two approaches for this kind of question. Both involve examining the range operator a bit more closely.

Option 1 is to catch the return value of the range operator itself, which you now only use as a boolean.

if(my $cnt = /^ policy-map $ouputpol/ ... / policy-map/) {
(Precedence of "..." is higher than the assignment.)

If you examine the contents of $cnt closely, you'll see it's a counter, with the numerical value 1 for the first line, 2 for the next... and as a string, it has a "E0" appended only for the very last line. So you can check it:

print unless $cnt==1 || $cnt =~ /E/;
Note that that suffix doesn't change the value of the number, while it's still a valid representation of it.

The second approach is to catch the return value of the left and right hand sides individually:

if(($my $first = /^ policy-map $ouputpol/) ... (my $last = / policy-ma +p/)) {
Note the extra parens. Here, $first will be true for the first line, and $last will be true for the last one. No additional check is necessary.
print unless $first || $last;

For the people who want a simple standalone test, without requiring something ressembling your data:

for my $i (1 .. 15) { if(my $cnt = $i==5 ... $i==10) { print "$i - $cnt\n"; } }
which prints:
5 - 1
6 - 2
7 - 3
8 - 4
9 - 5
10 - 6E0
And the other one:
for my $i (1 .. 15) { if((my $first = $i==5) ... (my $last = $i==10)) { # note: $first will be undefined for any but the first line be +cause of the 3 dots # and $last will be undefined for the first line local $^W; print "$i - $first / $last\n"; } }
which prints:
5 - 1 / 
6 -  / 
7 -  / 
8 -  / 
9 -  / 
10 -  / 1

In reply to Re: Matching between START and END revisited by bart
in thread Matching between START and END revisited by PenguinFeva

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.