I have two sets of ranges, represented by [ start, stop ] values. Some of the ranges overlap, meaning that the start of one range is in between the [ start, stop ] of the other range. I’d like to make a new set of ranges that has no such overlap, and also doesn’t include any new values in a range.
The ranges look like this:
@starts @ends
5 108
5 187
44 187
44 229
44 236
64 236
104 236
580 644
632 770
The output that I expect would be this:
@starts @ends
5 236
580 770
This is because the first seven ranges overlap with the interval from 5 => 236, and the last two overlap with the interval from 632 => 770.
Here’s the code that I tried:
$fix = 0;
foreach (@ends) {
if ($starts[$fix + 1] < $ends[$fix]) {
splice(@ends, $fix, $fix);
splice(@starts, $fix + 1, $fix + 1);
} else {
$fix += 1;
}
}
I can print out the values myself, I just need help with the algorithm for merging.
This edits your arrays in-place, simply collapsing boundaries when they overlap.