algorithm - Loop through two arrays deleting overlaps in perl -


i have 2 sets of ranges, represented [ start, stop ] values. of ranges overlap, meaning start of 1 range in between [ start, stop ] of other range. i'd make new set of ranges has no such overlap, , doesn't include new values in range.

the ranges this:

@starts  @ends       5    108        5    187      44    187      44    229       44    236       64    236      104    236     580    644     632    770 

the output expect this:

@starts  @ends       5    236     580    770 

this because first 7 ranges overlap interval 5 => 236, , last 2 overlap interval 632 => 770.

here's code 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 values myself, need algorithm merging.

this edits arrays in-place, collapsing boundaries when overlap.

# since they're sorted @starts, accept 0th interval, start @ 1 (1..$#starts) {     # check on array bounds, since edit in-place     last unless $_ < @starts;     # don't need collapse if no overlap previous end     next unless $starts[$_] <= $ends[$_-1];     # delete start , previous end     splice(@starts,$_,1);     splice(@ends,$_-1,1);     # rerun loop same value of $_ since deleted     redo; } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -