in reply to reading a line from file two times

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

Replies are listed 'Best First'.
Re^2: reading a line from file two times
by greatshots (Pilgrim) on Dec 21, 2006 at 03:11 UTC
    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"

        There's definitely someting screwy with using a negative, relative seek (on Win32). To back up over a line consisting of 8 chars + NL, requires seeking back -6.

        I'd expect to have to seek back more than the line length, not less to account for the newline?

        #! perl -slw use strict; while( <DATA> ) { chomp; print; if( /the line/ ) { my $backup = -length() + 2; print "Backing up $backup places"; seek DATA, $backup, 1; chomp( $_ = <DATA> ); print "?$_?"; } } =output c:\test>junk junk junk more junk the line Backing up -6 places ?the line? other junk yet more junk =cut __DATA__ junk junk more junk the line other junk yet more junk

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        seek(FH, (0 - length($line)+tell(FH)), SEEK_CUR);

        You are seeking too far back. You're seeking back the length of the string plus the byte-number of your current position...

        If you wish to use tell() to record the old position before your read, then you'd use: seek(FH, $old_position, SEEK_SET); instead.

        -Paul