#! perl -slw use strict; use Data::Dumper; my @ranges = ( [17,19], [34,39], [26,29], [53,57], [43,47], [58,59] , [40,45], [30,33], [20,24], [10,15], [6,9], [1,4], , [35,45], [7, 15], ); #! initialise a string of null as long as the string the ranges pertain to. my $coal = "\0" x 100; #! fill all the ranges with 1's substr($coal, $_->[0], $_->[1] - $_->[0] + 1) = 1 x ($_->[1] - $_->[0] + 1) for @ranges; #print $coal; #! Build the new AoA of ranges by scanning the string recording starts and length of string of 1's my (@new, $start); for (0 .. length $coal) { my $c = substr($coal, $_, 1); next if not $start and $c ne '1'; $start = $_ if $c eq '1' and not $start; next if $start and $c eq '1'; push @new, [$start, $_-1]; $start = undef; } print Dumper \@new; __END__ $VAR1 = [ [ 1, '4' ], [ 6, '15' ], [ 17, '24' ], [ 26, '47' ], [ 53, '59' ] ];