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

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 ?

Replies are listed 'Best First'.
Re^3: reading a line from file two times
by jettero (Monsignor) on Dec 21, 2006 at 03:35 UTC

    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

Re^3: reading a line from file two times
by jesuashok (Curate) on Dec 21, 2006 at 03:22 UTC
    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.
        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

      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