On 11/27/07, Giuseppe.G. <[EMAIL PROTECTED]> wrote:

> I have a text file, containing a number of varying length documents,
> all tagged with DOC-START and DOC-END, structured as follows:
>
>
> DOC-START
> content of the document
> DOC-END

> I need to take every document and pass it to a parsing function, that
> accepts a file with *a single* document,

> My file contains lots of these records, so picking each document and
> writing it into a new file is not an option. How can I extract the
> documents one at a time from the file? Is it possible e.g. to put one
> such record iteratively into a temporary file and pass this to the
> parser?

Sure, but you probably can do the job more simply. You could collect a
line at a time until you've reach the end of a document, then pass the
whole document to the subroutine at once. I'm imagining a program
looking roughly like this:

  my @document;
  while (<INPUT>) {
    push @document, $_ if @document or /DOC-START/;
    if (/DOC-END/) {
      &process(@document);  # or whatever you need
      @document = ();  # empty again
    }
  }
  warn "unexpected EOF" if @document;

Good luck with it!

--Tom Phoenix
Stonehenge Perl Training

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to