in reply to Next unused sequential number
It doesn't actually need the list to be sorted.my @uids = ....; # List of used UIDs my %uids; @uids{@uids} = (); my $uid = 1001; {$uid++, redo if exist $uids{$uid}} say "First unused uid: $uid";
Alternatively, you can make use of the fact the list is sorted (again, untested):
Note that the latter algorithm isn't Perl specific, and can be trivially ported to many general purpose languages.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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Next unused sequential number
by Anonymous Monk on Aug 05, 2011 at 18:17 UTC |