Well, I cant see anything obviously wrong with this, except that it uses unobvious methods to accomplish its task and is therefore difficult to understand. If I understand you right this is the opposite of the 'rangification' problem that pops up in various places on a regular basis. In that problem one wishes to take a set of numbers and convert them into a format of begin-end. Your problem (if I understand it correctly) is even easier. You wish to go through a list of numbers to find missing elements in the list. So as I couldnt see what was wrong I decided Id write a quick sub to do the same thing but is a bit more obvious (and less of a memory hog, and presumably faster to boot :-) Note that there is no need for the hash nor the grep.
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 mis
+sing
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
Apologies if Ive completely missed the point of your routine.
HTH
Yves / DeMerphq
--
Have you registered your Name Space?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.