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

Upon adding records to a delimited text file, I need to append a number to the end of each record each time a record is added to the file. How can I increment this "record #" each time so each is unique?

Replies are listed 'Best First'.
Re: increment record number
by fundflow (Chaplain) on Oct 18, 2000 at 04:02 UTC
    According to your description, the maximal value is at the end. So, when you open the file to add a new record, seek back enough and read the last number. Add one to that and store it next.


    This example assumes that your record is at most 100 characters and that each record is white-space separated:
    open(F,'myfile') or die; $/='RECORD SEPARATOR'; seek F, -(101+length($/)), SEEK_END; <F>; while(<F>) { $max=(split)[-1]; } print "New value can be ", $max+1, "\n";
    (untested)

    HTH
Re: increment record number
by merlyn (Sage) on Oct 17, 2000 at 21:21 UTC
    Why are you storing a computed value into the file? Can you not make your "reading" algorithm just compute the line number upon read? That would seem more efficient, and less likely to get out of sync later.

    -- Randal L. Schwartz, Perl hacker

Re: increment record number
by Fastolfe (Vicar) on Oct 17, 2000 at 21:26 UTC
    Two options:
    1. Reserve a line in your file to store the 'next record number'. Increment this number for each record you add.
    2. You'll just have to read through the file and either:
      • Parse each record, retrieve the record number, and keep track of the largest number you see. Increment and use that as your new record number. This is probably the safest approach.
      • If you're guaranteed to have the same number of records as lines in the file (i.e. you won't be deleting lines without renumbering), you can just count the lines and add 1.
    Either way, you're looking at potential problems if multiple processes attempt to update the file at the same time, so look into flock or some equivalent to be sure only one update is done at a time.
Re: increment record number
by turnstep (Parson) on Oct 17, 2000 at 21:23 UTC

    If you are using a database, most have some way to auto-increment a number. Otherwise, you'll have to read in the number from your previous entry, increment it by one (or whatever you are doing to ensure a "unique" value), then write the new record. And you'll have to carefully lock the file to make sure that nobody else increments the number while you are using it.

RE: increment record number
by Albannach (Monsignor) on Oct 17, 2000 at 21:39 UTC
    If you've considered all the above good advice and you still want to store record number in the text file, you could save yourself some line-counting time by keeping the last-used number in a configuration file (or *gasp* the Registry if you're suffering from Windows).

    You still have the problem of stopping other processes from messing you up though!