in reply to Re^2: reading a line from file two times
in thread reading a line from file two times

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"

Replies are listed 'Best First'.
Re^4: reading a line from file two times
by BrowserUk (Patriarch) on Dec 21, 2006 at 04:38 UTC

    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.
      Should be on systems where seek($fh, $amount, SEEK_CUR) works correctly, you'd have to backup over all the characters read. So if the length($entire_string) is 10 (including LF or CRLF), that'd be: seek($fh, -10, SEEK_CUR). I do seem to recall that it behaves unexpectedly (for a unix nerd) in windows, but my experience was in foxpro, not perl — that definitely shouldn't count.

      -Paul

Re^4: reading a line from file two times
by jettero (Monsignor) on Dec 21, 2006 at 03:45 UTC

    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