Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Finding missing elements in a sequence (Rewritten code)

by demerphq (Chancellor)
on Nov 06, 2001 at 22:01 UTC ( [id://123641]=note: print w/replies, xml ) Need Help??


in reply to Finding missing elements in a sequence (code)

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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://123641]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-18 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found