Maybe I'm recreating the wheel here, but wouldn't the following work for you?
#!/usr/bin/perl my @data = ( [ 2, 8 ], [ 1, 3 ], [ 4, 6 ], [ 7, 10 ], [ 8, 20 ], ); foreach my $pair (@data) { @{$pair}[1,0] = @{$pair}[1,0] if $pair->[0] > $pair->[1]; $pair->[2] = $pair->[1] - $pair->[0]; } @data = sort { $a->[2] cmp $b->[2] || $a->[0] cmp $b->[0] || $a->[1] c +mp $b->[1] } @data; my @good; PAIR: foreach my $pair (@data) { my @check; foreach my $i (0 .. $#good) { my $good = $good[$i]; if ( ($good->[0] <= $pair->[0] && $pair->[0] < $goo +d->[1]) || ($good->[0] < $pair->[1] && $pair->[1] <= $goo +d->[1]) || ($pair->[0] <= $good->[0] && $good->[0] < $pai +r->[1]) || ($pair->[0] < $good->[1] && $good->[1] <= $pai +r->[1]) ) { push @check, [ $good, $i, ]; } } unless (@check) { push @good, $pair; next PAIR; } my $max = 0; $max += $_->[2] for map { $_->[0] } @check; next PAIR if $max > $pair->[2]; splice @good, $_, 1 for sort { $b <=> $a } map { $_->[1] } @ch +eck; push @good, $pair; } @good = sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @good; print "$_->[0] ... $_->[1] ($_->[2])\n" for @good; my $cover = 0; $cover += $_->[2] for @good; print "Total coverage is $cover\n";

------
We are the carpenters and bricklayers of the Information Age.

The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.


In reply to Re: Comparing 2-D co-ordinates by dragonchild
in thread Comparing 2-D co-ordinates by aging acolyte

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



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.