----- Original Message ----- 
From: "Larry Wissink" <[EMAIL PROTECTED]>
Newsgroups: perl.beginners
To: <[EMAIL PROTECTED]>
Sent: Wednesday, May 12, 2004 6:39 PM
Subject: Finding missing numbers in sequence


I have a problem that I thought would be perfect for Perl, except that I
seem to be using all my system resources to run it.  Of course this
probably means I'm doing it the wrong way...



The problem:

We have a backup server that is missing records from the production
server for a particular table.  We know that it should have sequential
records and that it is missing some records.  We want to get a sense of
the number of records missing.  So, we know the problem started around
the beginning of March at id 70,000,000 (rounded for convenience).
Currently we are at 79,000,000.  So, I dumped to a file all the ids
between 70,000,000 and 79,000,000 (commas inserted here for
readability).  I need to figure out what numbers are missing.  The way
that seemed easiest to me was to create two arrays.  One with every
number between 70 and 79 million, the other with every number in our
dump file.  Then compare them as illustrated in the Perl Cookbook using
a hash.


So, does anybody have a suggestion for a better way to do it in Perl?

 Hello Larry,

I was able to come up with a simple script that doesn't require using an
array/hash, so that your mem problems should not occur.

NOTE: the beginning number has to have 1 subtracted from it. This is a first
case situation so that that the algorithm works for the set. Also, if there
are missing records after the last number read in, it won't find that
sequence, but that should be easy enough to find (in this example, any
numbers after 20).

HTH
Chris


#!/usr/bin/perl
use strict;
use warnings;

my @missing;

my $i = 0; # Or 70,000,000 - 1 (69,999,999)

while(<DATA>) {
     if ($_ != ++$i) {
          push @missing, $i .. $_ - 1;
          $i = $_;
     }
}

print join "\n", @missing;

__DATA__
4
5
7
10
12
13
14
15
20


OUTPUT
1
2
3
6
8
9
11
16
17
18
19



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to