develooper Front page | perl.fwp | Postings from July 2003

range operator for a condition in 2 steps

Thread Next
From:
Bart Lateur
Date:
July 30, 2003 19:06
Subject:
range operator for a condition in 2 steps
Message ID:
untgiv40ujcoif9r83fjsfibighfa7lmda@4ax.com
What I would want is to match lines in a loop, one line at a time, say,
between a line cotnaining just "START" and a line containing just "END",
but where the latter condition is only looked at, after an intermediate
condition has been met first. In particular, I have to have seen a blank
line first.

Looking at the skeleton program below, I would like as output to see
these lines printed:

 1) from just underneath "2" till just above "4", blank line under "3"
 2) from just underneath "6" till just above "14", blank line under "11"
(The line just above "9" should be ignored)
 3) from just underneath "17" till just above "20", blank line under
"19"

and that using an expression that works pretty much like the ".." and/or
"..." operators would. You see, I like the layout of the skeleton
program.

I found a working (but quite ugly) solution, but I'll let you people
chew on it first. You'll likely come up with something better, anyway. 

Bart.

Here's the skeleton program:

#!/usr/local/bin/perl
while(<DATA>) {
    if(/^START$/ .. /^END$/) {  # edit this condition
        print;
    }
}
__DATA__
1
2
START
3

END
4
5
6
START
7
8
9
END
10
11

12
13
END
14
15
16
17
START
18
19

END
20
21
22


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About