For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2007 > Re: Extracting one record from multiple-records textfile









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Extracting one record from multiple-records textfile
John W . Krahn

2007-11-27, 10:01 pm

On Tuesday 27 November 2007 11:29, Giuseppe.G. wrote:
> Hello there.


Hello,

> I'm a real beginner and I was wondering if you could be
> so kind to help me on this.
> 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
>
> DOC-START
> some text
> DOC-END
>
> DOC-START
> rtreytgfbvb
> DOC-END
>
> DOC-START
> sdfdf fdff ee
> DOC-END
>
> ...
>
> I need to take every document and pass it to a parsing function, that
> accepts a file with *a single* document, that is
>
> DOC-START
> some text
> DOC-END
>
> 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?



# Set the Input Record Separator
$/ = "\nDOC-END\n";

while ( my $doc = <FILE_HANDLE> ) {
parsing function( $doc );
}




John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com