Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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?


In reply to Re: Finding missing elements in a sequence (Rewritten code) by demerphq
in thread Finding missing elements in a sequence (code) by deprecated

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found