Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to read one page at a time? 3

Status
Not open for further replies.

TechieTik

Programmer
Sep 27, 2006
5
US
I have a text file that contains several pages with page break. I need to open this file and read one page (marked by page break charecter) into a buffer variable and based on certain conditions need to write (the entire page in buffer) to one of the several output files. How do I do it? I've used very basic file operations in perl but this type operations.

Thanks to the great forum that helps many new people like me.

TX
 
You could try re-defining Perl's default record seperator $/ to be a page break (not sure how the program reads it).

with the default RS set to the standard newline this command:
Code:
while (<FILE>) {
   push (@results, $_);
}

Will push each line into an array. If you re-define the default record seperator as in:

Code:
$/ = (whatever a page break is);
while (<FILE>) {
   push (@results, $_);
}

Now each array element will be a page.

Someone please correct me if I am wrong.

Nick

If at first you don't succeed, don't try skydiving.
 
How about loading the whole file like this:

open ( FIL, "<", "filename.ext" );
$data = <FIL>;
close ( FIL );

...then using the "index" function to take slices of the data in a for loop?

 
@AMISM - that will only read the entire file if you've set $/ to be undefined first, otherwise you'll only get the first line.
 
Thanks Nick(netman4u) for the suggestion. That worked fine.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top