Update:
I guess I didn't make the idea very clear. There are of course many way to tackle this problem and as the original OP suggested, I could have coded a set-a-flag or retain-a-token solution and posted it there.
The idea came to me that if you want to extract a range of lines from a file, you are often advised to use the flip-flop operator
perl -nle "100 .. 200 and print;"
Neat, quick and simple.
This particular problem seemed analogous to me. You want to start capturing at some point and stop at another. I had envisaged that something like
while( @data ) { /^HEADING/ ... !/^HEADING/ and push @headings, $_; /^TITLE/ ... !/^TITLE/ and push @titles, $_; /^COMPND/ ... !/^COMPND/ and push @cmpnds, $_; }
might be possible. And that this could be generalised into
$re_types = qr/(HEADING|TITLE|COMPND)/; while (@data) { /^$re_types(.*)$/ ... !/^$1/ and push @{$type{$1}}, $2; }
Which I think would be useful and usable.
The OP's requirements to catenate the lines complicates this somewhat, but the idea still struck me interesting. However, I still have problems with using the bi-stable operator with other than constant parameters and thought that by posting it here in its half finished state someone might see how to move the idea on from where I came unstuck.
So, the following code isn't attempting to solve that OP's original problem, nor is it presented as a working or usable piece of code hence the "needs work" in the title. its purpose is purely to serve as a demonstrator for an idea and the problems encountered trying to implement it.
Maybe its simply a solution looking for a problem that can be solved other ways, but I like TIMTOWTDI ;)
</update>
This started off as a tentative, generalised solution to Finding first block of contiguous elements in an array, but it's way to messy and inscrutable to post there, but I think it has potential.
It is currently, as Aristotle has a habit of saying about my kludges--Yuk!
If your one of those people... (Shades of "It'll be Allright on the Night" :^)... that has done all their Xmas shopping and is sitting at home on a wet weekend with nothing to do, maybe you can see how to generalise and tidy this up.
In particular, if anyone can explain why the last (compound) statement in the loop acts differently from the first two such statements, cos it's got me foxed.
#! perl -slw use strict; my @data = <DATA>; my (@headers, @titles, @compnds); for (@data) { (/^HEADER (?:\d+ )?(.*?)$/ and push(@headers, '' )) ... !/^HEADER (?:\d+ )?(.*?)$/ and $1 and $headers[-1] .= $1, next; (/^TITLE (?:\d+ )?(.*?)$/ and push(@titles, '' )) ... !/^TITLE (?:\d+ )?(.*?)$/ and $1 and $titles[-1] .= $1, next; (/^COMPND (?:\d+ )?(.*?)$/ and push(@compnds, '' )) ... !/^COMPND (?:\d+ )?(.*?)$/ and $1 and $compnds[-1] .= $1; } print for @headers; print for @titles; print for @compnds; __DATA__ HEADER Header 1 stuff TITLE Title 1 stuff TITLE 2 more title 1 stuff COMPND complicated stuff 1 COMPND 2 continued complicated stuff 1 HEADER Header 2 stuff TITLE Title 2 stuff TITLE 2 more title 2 stuff COMPND complicated stuff 2 COMPND 2 continued complicated stuff 2 HEADER Header 3 stuff TITLE Title 3 stuff TITLE 2 more title 3 stuff COMPND complicated stuff 3 COMPND 2 continued complicated stuff 3 COMPND 3 continued complicated stuff 3 HEADER Header 4 stuff TITLE Title 4 stuff TITLE 2 more title 4 stuff COMPND complicated stuff 4 COMPND 2 continued complicated stuff 4 HEADER Header 5 stuff TITLE Title 5 stuff TITLE 2 more title 5 stuff COMPND complicated stuff 5 COMPND 2 continued complicated stuff 5 HEADER Header 6 stuff TITLE Title 6 stuff TITLE 2 more title 6 stuff COMPND complicated stuff 6 COMPND 2 continued complicated stuff 6
Outputs
C:\test>221570 Header 1 stuff Header 2 stuff Header 3 stuff Header 4 stuff Header 5 stuff Header 6 stuff Title 1 stuff 2 more title 1 stuff Title 2 stuff 2 more title 2 stuff Title 3 stuff 2 more title 3 stuff Title 4 stuff 2 more title 4 stuff Title 5 stuff 2 more title 5 stuff Title 6 stuff 2 more title 6 stuff complicated stuff 1 2 continued complicated stuff 1 complicated stuff +2 2 continued complicated stuff 2 complicated stuff 3 2 continued com +plicated stuff 3 3 continued complicated stuff 3 complicated stuff 4 2 continued complicated stuff 4 complicated stuff +5 2 continued complicated stuff 5 complicated stuff 6 2 continued com +plicated stuff 6 C:\test>
I've broken the last bit up for posting but not well to show that the problem with the last statement in the loop.
Examine what is said, not who speaks.
In reply to An (almost) useful idiom; needs work. by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |