jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

monks,

I am reading a line Example :-

$line = "NEWTABLE"
I want to read this line again. how can I move the cursort position using seek function ?

Replies are listed 'Best First'.
Re: reading a line from file two times
by davido (Cardinal) on Dec 21, 2006 at 03:05 UTC

    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

Re: reading a line from file two times
by jettero (Monsignor) on Dec 21, 2006 at 03:08 UTC
    I'm assuming you assigned a value, $line = <$file>; print "yeah\n" if $line eq "NEWTABLE$/"?

    If so, the answer is simply  use Fcntl qw(:seek); seek($file, (0 - length $line), SEEK_CUR) or die $!;

    UPDATE: Oh, that's interesting! First answer is an absolute method and the second is relative. Hehe. davido also linked the two important docs. They're both worth a careful read at this point.

    -Paul

      hi jettro,
      if ( $line =~ /NEWTABLE/) { print "b4 seek $line\n"; print "len :$length:\n"; seek(FH, (0 - length($line)), SEEK_CUR); chomp($line=<FH>); print "--> $line\n"; exit; }
      The above coed still prints the next line after using seek. am I missing something there in the seek function ?

        The above coed still prints the next line after using seek. am I missing something there in the seek function ?

        The negative seek may not work in windows... not sure. It definitely does work on my linux box though. I don't have any windows handy or I'd test it. I did notice you didn't use "or die $!. " Most likely it would return an error result on your platform.

        use strict; use Fcntl qw(:seek); open my $in, "filename" or die $!; my $line = <$in>; print "$line\n"; seek $in, (0 - length $line), SEEK_CUR or die $!; $line = <$in>; print "$line\n"; close $in;

        It should either work or complain...

        -Paul

        Hi

        if ( $line =~ /NEWTABLE/) { seek(FH, (0 - length($line)+tell(FH)), SEEK_CUR); $line = <FH>; chomp($line); print "LINE AFTER SEEK :$line:\n" }
        Output :-
        "LINE AFTER SEEK :EWTABLE:\n"
        why I am missing 'N' after the seek ?

        "Keep pouring your ideas"
Re: reading a line from file two times
by swampyankee (Parson) on Dec 21, 2006 at 17:50 UTC

    You've had help from the other Monks. I've got a question:
       Why do you want to do this?

    I've found relatively few reasons for doing something like this, and most involved error handling in languages which were sensitive to the type of data being read (usually something to the effect of

    read(1,'(i4)', iostat = irc) integer_value</p> if(irc /= 0) then backspace(1) read(1,'(a)') string write(*,*)'error number',irc,'reading ',string,' Bye!' call abort() endif

    I don't understand the need to do something like this in Perl.

    emc

    At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

    —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.