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

Hi, What I'm doing is print'ing some entries to a file.
NAME:name ID:id TIME:time ...
At some point, I decide that the time value is too old, and since I've already printed the previous two lines to the file I now want to delete them. Is there an easy way of doing this other than checking the time before I print those lines to the file? Is there an "unprint"? Thanks!

Replies are listed 'Best First'.
(chromatic) Re: Deleting a line from a file
by chromatic (Archbishop) on Jul 14, 2000 at 06:16 UTC
    If you know the record length and can calculate file offsets, you can use a combination of seek and syswrite to overwrite chunks of a file. Of course, since it's unbuffered and print is buffered, you can really get tied up in knots.

    I'd just check the time before you print. It's a lot easier.

      As we know from perl documentation, this is strongly deprecated to use both print and syswrite to the same filehandle.
      It is much safer to either reopen filehandle in a different way, or suspend writing until exact output is known (for example use stack).

      It is often to not do undesireable things instead of doing them and then trying to undo.

Re: Deleting a line from a file
by c-era (Curate) on Jul 14, 2000 at 15:32 UTC
    Here is a quick hack I put together (I assumed that you only have NAME, ID, and TIME as fields).
    my $time = 50; while (<IN>){ @tmp_split = split /:/,$_; # Print the previous entry if valid print OUT "NAME:$tmp_hash{\"NAME\"}ID:$tmp_hash{\"ID\"}TIME:$tmp_hash{ +\"TIME\"}" if ($tmp_split[0] eq "NAME" && $tmp_hash{"TIME"} > $time); # Put the values in a hash for storage $tmp_hash{$tmp_split[0]}=$tmp_split[1]; } # Print the last entry if valid print OUT "NAME:$tmp_hash{\"NAME\"}ID:$tmp_hash{\"ID\"}TIME:$tmp_hash{ +\"TIME\"}" if ($tmp_hash{"TIME"} > $time);
    I hope that helps.