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

What's the best/smartest way to grab lines from a file and discard them from the file after they've been used?

I want to be able to open it up, use a few lines, delete those lines and close it, so that when I come to use that file again I'm starting from the next unused line.

I could of course read the whole thing into an array, shift off the lines then write the remainder back, but there must be a more elegant way -- should I Tie this file in some way? Or have I missed something obvious?



($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
=~y~b-v~a-z~s; print

Replies are listed 'Best First'.
Re: Shifting the top line(s) of a file?
by blue_cowdawg (Monsignor) on Jul 18, 2004 at 00:37 UTC

        What's the best/smartest way to grab lines from a file and discard them from the file after they've been used?

    As already indicated by beable, here is good solution:

    #!/usr/bin/perl -w ############################################### use strict; use Tie::File; my @ry=(); tie @ry,"Tie::File","file2use.txt" or die $!; my $line = shift @ry; untie @ry;
    Viola! The top line will disappear.

Re: Shifting the top line(s) of a file?
by beable (Friar) on Jul 17, 2004 at 23:35 UTC
Re: Shifting the top line(s) of a file?
by pg (Canon) on Jul 18, 2004 at 00:03 UTC

    An alternative way that has a better performance (as it uses less expansive file operations):

    1. Instead of using one file, use a series of files. Numbering them, so they come in sequence.
    2. have a size limitation on files. You always append to the last file, but when it reaches its size limitation, create a new file.
    3. when you read, you always read from the first file, and remember the last line you read, next time, just continue from there. Once all lines are read, delete the first file, and go to the new first (by having some simple numbering mechanism, you will always know which file is the first.)
Re: Shifting the top line(s) of a file?
by Fletch (Bishop) on Jul 18, 2004 at 02:21 UTC

    If you can accept a fixed number of fixed sized entries you could (with appropriate locking, of course) make a ring buffer. Store the head and tail offsets as the first record and then use something along the lines of seek( RING, $rec_num * REC_SIZE() + OFFSETSIZE(), SEEK_SET() ) and sysread( RING, $buf, REC_SIZE() ) to retrieve them.

Re: Shifting the top line(s) of a file?
by thospel (Hermit) on Jul 18, 2004 at 13:13 UTC
    The basic problem is that for most commonly used filesystems there basically is no API to remove things from the front (or the middle). Modules like Tie::File only help in the sense that they do things in a smarter way than just an array read and write, but they still work by basically rewriting everything after your deletepoint.

    So if you do this kind of stuff on small files, Tie::File is a fine solution. If the files get big, you'd better rethink your storage format. E.g. use multiple files with a given maximum size, or use a dbm file, or go for a SQL database or so. Or if you don't mind that old data remains in the file, simply have a companion file that remembers the seek position of where you got last time, or even have this data in a fixed size record at the start of your file (there are ways to overwrite a range of bytes with the same amount of (different) bytes.

Re: Shifting the top line(s) of a file?
by Anonymous Monk on Jul 22, 2004 at 04:13 UTC
    i believe it is the nature of filesystem even some SQL database does not actually "cut" a line/record if you delete/remove it, it just rewrites that line with straight whitespace, remember that line, insert data onto that line the next time someone writes something to it the only way seems to be using fixed length record to store your data, and in the ideal case using a few seek() can get you to the line