sub find_holes { my $list = shift; my @list = sort { $a <=> $b } @$list; my $last = $list[0]; my $l = length($last); my @vacancies; foreach my $v (@list) { #If the difference is larger than 1 then theres a hole if ( $v - $last > 1 ) { push @vacancies, sprintf( "%0${l}d", $_ ) #store whats missing for ( $last + 1 .. $v - 1 ); } $last = $v; #keep track of the last number we saw } return \@vacancies; # return the omitted elements } print join "\n", @{find_holes( [ '00001', '00002', '00005', '00006', '00009', '00010', '00013', '00025' ] )}; __END__ (edited) Output: 00003 00004 00007 00008 00011 00012 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023 00024