in reply to Next unused sequential number

Unless you have an array containing billions of uids, I'd do (untested):
my @uids = ....; # List of used UIDs my %uids; @uids{@uids} = (); my $uid = 1001; {$uid++, redo if exist $uids{$uid}} say "First unused uid: $uid";
It doesn't actually need the list to be sorted.

Alternatively, you can make use of the fact the list is sorted (again, untested):

my @uids = ....; # List of sorted uids my $uid = 1001; for (my $i = 0; $i < @uids; $i++) { next if $uids[$i] < $uid; last unless $uids[$i] == $uid; $uid++; } say "First unused uid: $uid";
Note that the latter algorithm isn't Perl specific, and can be trivially ported to many general purpose languages.

Replies are listed 'Best First'.
Re^2: Next unused sequential number
by Anonymous Monk on Aug 05, 2011 at 18:17 UTC
    Thank you for your collective wisdome perl monks. I found This to be quite helpful:
    Alternatively, you can make use of the fact the list is sorted (again, + untested): my @uids = ....; # List of sorted uids my $uid = 1001; for (my $i = 0; $i < @uids; $i++) { next if $uids[$i] < $uid; last unless $uids[$i] == $uid; $uid++; } say "First unused uid: $uid";
    My day is going well now!