[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl-AnyPerl] Reset File



At 12:49 PM -0700 5/25/99, Mark Yannuzzi wrote:
>Is there a 'reset' file command, or option for OPEN in Perl, or do you need
>to CLOSE and OPEN the file again if you wish to read it line-by-line from
>the beginning?
>
>I need to count the number of occurrences of a string in a file first,
>before I can process the files contents, so I need to be able to 'point' to
>the beginning of the file after I have reached the EOF.
>
>In looking through the Camel book, it appears that the 'seek' function would
>be the way to do this:
>
>seek FILEHANDLE,0,0;

There's more.

Haven't had to do this very often, but here's my working knowledge:

In the first pass through the file, when you find and count each
occurrence, use 'tell' to get its (byte) location in the file. Then you can
use 'seek' to start over as above, and *also* to return for processing to
each occurrence whose location you stored in the first pass. You can build
and utilize an efficient index to your file using these functions (put the
index in a hash or a file).

Now, others may correct or add examples...

- Bruce

# ~~~~~~~~~~~~~~~~~~~
# Bruce Van Allen
# bva@cruzio.com
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Director
# WireService, an Internet service bureau
# Serving the educational and nonprofit sectors
# wire@wireservice.org
# http://wireservice.org
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-anyperl-request@macperl.org