I would use
Bit::Vector for this. You'll need about 125MB of free memory, but that's easy to come by these days. Should work fine for overlapping ranges and runs very fast on my system:
use Bit::Vector;
use strict;
use warnings;
my $vector = Bit::Vector->new(1_000_000_000);
$vector->Fill();
my @ranges = ([39887, 399900],
[23, 900],
[8000, 10000],
[50000, 1000500]);
$vector->Interval_Empty($_->[0] - 1, $_->[1] - 1) for @ranges;
my $start = 0;
while ($start < $vector->Size()
and my ($min, $max) = $vector->Interval_Scan_inc($start))
{
print(($min+1), "-", ($max+1), "\n");
$start = $max + 2;
}
Prints:
$ perl bits.pl
1-22
901-7999
10001-39886
1000501-1000000000
You might also look at Bit::Vector's to_Enum() and from_Enum() method, which do IO using ranges of indexes but it in a slightly different format from yours.
-sam
-
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.