in reply to Next unused sequential number

slightly different formulation, but same idea of linear search. sort the array if it might not be in order.
#!/usr/bin/perl -w use strict; my @x = (1001,1002,1003,1004,1006,1007,1009,1010); print get_next(\@x); #1005 sub get_next { my $aref = shift; my $cur; foreach (sort{$a<=>$b}@$aref) { if (!defined $cur){$cur=$_; next;} return $cur if (++$cur ne $_); } return undef; #search failed - no holes! }