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)