Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think that this is what you want. You have a series of ranges of the form [start,stop], and you'd like to merge the overlapping ranges. The approach below is fairly simple. </p> <ol> <li>There are two sets of ranges, the original set and the merged set. </li> <li>You add the first range to the set of merged (non-overlapping) ranges. For each candidate range left from the original set, you make a choice: <ul> <li>If that candidate overlaps with one already in the merged set, you extend the boundaries of the range in the merged set appropriately. </li> <li>If there is no overlap between the candidate range and any range in the merged set, you add the candidate to the merged set.</li> </ul></li> </ol> <p>Hopefully this makes sense. It's not too obvious from your question that this is what you wanted, so let me know if this isn't right. </p> <pre><code>#!/usr/bin/perl use strict; use warnings; my @starts = qw/ 5 5 44 44 44 64 104 580 632 /; my @ends = qw/ 108 187 187 229 236 236 236 644 770 /; my @ranges; while ( @starts &amp;&amp; @ends ) { my $s = shift @starts; my $e = shift @ends; push @ranges, [ $s, $e ]; } my @merged_ranges; push @merged_ranges, shift @ranges; foreach my $range (@ranges) { my $overlap = 0; foreach my $m_range (@merged_ranges) { if ( ranges_overlap($range,$m_range) ) { $overlap = 1; $m_range = merge_range($range,$m_range); } } if ( !$overlap ) { push @merged_ranges, $range; } } print join ' ', qw/ start end /; print "\n"; foreach my $range (@merged_ranges) { print join ' ', ( $range-&gt;[0], $range-&gt;[1] ); print "\n"; } sub ranges_overlap { my $r1 = shift; my $r2 = shift; return ( $r1-&gt;[0] &lt;= $r2-&gt;[1] &amp;&amp; $r2-&gt;[0] &lt;= $r1-&gt;[1] ); } sub merge_range { my $r1 = shift; my $r2 = shift; use List::Util qw/ min max/; my $merged = [ min($r1-&gt;[0],$r2-&gt;[0]), max($r1-&gt;[1],$r2-&gt;[1]) ]; return $merged; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload