Re: How to Read Files? [was: Re: learning perl 3rd vs 4th]
[email protected] (AndrewMcHorney)
| Newsgroups | perl.beginners |
|---|---|
| Message-ID | <[email protected]> |
Hello
The spec does not allow for carriage returns or line feeds making it
a long line. Is there a way to read "x" number of bytes?
Andrew
At 11:10 AM 5/25/2008, Rob Dixon wrote:
>Hi Andrew
>
>(If you are asking a new question of this group please make a fresh
>post with an
>appropriate subject line rather than replying to the end of an old
>thread. Many
>of us have email clients that correctly display the flow of threads, and using
>reply makes it look as if you have something more to say about a
>previous topic.
>Thanks.)
>
> > I have been reading files with the following command:
> >
> > @source_lines = (<SOURCE_FILE>);
> >
> > This has worked until I started reading some very large files.
>
>This reads the entire file into the array, and should only be used for files
>that you can guarantee to be very small. As you have found, filling up your
>computer's memory isn't a good idea!
>
> > Is there a better way to read files? The files I am trying to read now are
> > about 300 megabytes in size.
>
>In most cases you will need access to only one record at a time.
>Writing a loop
>like this
>
>while (<SOURCE_FILE>) {
> chomp;
> process_record($_);
>}
>
>will minimize your memory usage and almost certainly run much faster.
>
> > The bad news is that the spec has no carriage returns or line
> feeds. We have
> > no control over the spec. I need to replace a few characters in the file.
>
>The end of each file record must be defined somehow. Either there is a
>terminating characters sequence or the records are fixed-size, or
>possibly they
>are variable-sized with the actual size specified earlier in the file. Take
>another look at your file spec.
>
>HTH,
>
>Rob
>
>
>--
>Internal Virus Database is out-of-date.
>Checked by AVG Free Edition.
>Version: 7.5.516 / Virus Database: 269.8.0/817 - Release Date:
>5/24/2007 4:01 PM