in reply to reading a line from file two times
Prior to reading the line the first time, call tell. That will give you the current position in the file. Then after reading the line, you can call seek using the data you already retrieved with tell() to rewind back to the same line.
Another alternative is to use Tie::File to handle the details for you, and just treat the file like an array, which makes it much easier to move back and forth between various lines.
Update:
I just wanted to provide an example of my method (which jettero refers to as an absolute method):
use strict; use warnings; open my $fh, '<', 'testfile.txt' or die $!; my $position = tell $fh; while ( defined( my $line = <$fh> ) ) { print "First read:\t $line"; seek $fh, $position, 0 or die $!; my $reread = <$fh>; print "Second read:\t $reread"; } continue { $position = tell $fh; } close $fh;
The preceding snippet opens a file, reads and prints each of its lines twice. Of course it's sort of pointless; once you've read a line one time why read it again? It's easier to store the read value than to re-read it. Storing your previous position is just as much work as storing the read value, while storing the read value avoids two IO calls. But that's beside the point. ;)
Dave
|
|---|