How do I return to the top of a file without closing and reopening?

J

Joseph Ellis

Hello all.

I am currently writing a program that opens a file, searches for and
records a few lines into an array, then processes the lines in the
array in preparation for output.

However, some of the information that may be encountered in this array
may require the program to go back to the top of the file, and begin
searching for this new information.

Quick example:

open (FILE, "textfile.txt");
my $found;

while (<FILE>) {
if (/desiredinfo/) {
$found = 1;
push ($_, @array);
next;
}
if ($found and /nomoredesiredinfo/) {
last;
}
}

foreach (@array) {
if (/ineedtolookthroughtheFILEagain/) {
while(<FILE>) {
# Resumes reading through the FILE
# at the point where it left off at
# the end of the while loop, but
# I'd like to return to top of file
# instead.
if (/newinformation/) {
do_whatever();
}
}
}
}

close FILE;

Does this make sense? I'd like to return to TOF without having to
close and reopen the file for every element in @array that warrants
it.

I've read through perlfaq5 and ran every keyword I could think of
through perldoc -q. I presume that closing the file and reopening it
would have the desired effect, but wouldn't that be rather slow and
cumbersome?

Thanks for any suggestions.

Joseph
 
T

Tassilo v. Parseval

Also sprach Joseph Ellis:
I am currently writing a program that opens a file, searches for and
records a few lines into an array, then processes the lines in the
array in preparation for output.

However, some of the information that may be encountered in this array
may require the program to go back to the top of the file, and begin
searching for this new information.

Quick example:

open (FILE, "textfile.txt");
my $found;

while (<FILE>) {
if (/desiredinfo/) {
$found = 1;
push ($_, @array);
next;
}
if ($found and /nomoredesiredinfo/) {
last;
}
}

foreach (@array) {
if (/ineedtolookthroughtheFILEagain/) {
while(<FILE>) {
# Resumes reading through the FILE
# at the point where it left off at
# the end of the while loop, but
# I'd like to return to top of file
# instead.
if (/newinformation/) {
do_whatever();
}
}
}
}

close FILE;

Does this make sense? I'd like to return to TOF without having to
close and reopen the file for every element in @array that warrants
it.

Use seek() and the appropriate constants from the Fcntl module for
portability:

use Fcntl qw/:seek/;
...
seek FILE, 0, SEEK_SET;
I've read through perlfaq5 and ran every keyword I could think of
through perldoc -q. I presume that closing the file and reopening it
would have the desired effect, but wouldn't that be rather slow and
cumbersome?

Yes, so use seek(). See 'perldoc -f seek'.

Tassilo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top