Here's a different approach: seeing that your ID numbers are apparently not pre-assigned, and that you just create a new ID based on the number of records (but the ID really doesn't have any relation to the number of records), how about creating an ID that will undoubtedly be unique at the time it is created and even after thousands and thousands more ID's are created? Such as an ID based on the date/time. Just use the time since epoch, which is just the date/time in seconds from a specific date (which is 1Jan1970 on Unix systems I think).

sub get_current_epoch { use Time::Local; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$CURRENT,$isdst)=localt +ime(time); $EPOCH= timelocal($sec, $min, $hour, $mday, $mon, $year); return $EPOCH; }
This may cause problems with duplicate ID numbers if your situation were such that it was possible (i.e. like in a website) that more than one ID number were being created at the EXACT same second (their epoch time would be exactly the same). The solution to this would be to add randomly generated numbers to your ID in addition to the date/time. The more random digits you add, the less likely you are to have duplicates. For example, if it were possible in your situation that 2 ID's might be created at the same second, but no more than 2, then it would be useless to have 10 random digits added to your ID, but maybe 2 or 3 digits would be fine.

my $random = rand 999; #seeds and gets a random no. up to 999 $random = sprintf("%0.3d",$random); #fills in leading zeros $my ID = &get_current_epoch . $random; #uses above subroutine
Hope this all makes sense. The drawback is that the ID numbers are large, and it may be overkill for your situation. The advantage is that when you create a new ID, you just create it without having to check the latest entry, or for duplicate ID's. Plus you will have an automatic timestamp of when the record was created.

(12Mar2001 - code added for clarity)

In reply to Re: Storing Info in a text File by fpi
in thread Storing Info in a text File by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.