I don't quite understand what you mean by "coloured portions of a string"? What color would a peice of the string be if a red range wholey contained a yellow portion? Does the yellow become red or does the red range get split into two red ranges either side of the yellow range?
Anyway, assuming that the ranges are held in a AoA, this version should work pretty quickly and is really simple.
#! 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 perta +in 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' ] ];
Examine what is said, not who speaks.
The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.
In reply to Re: Overlapping portions of sub strings
by BrowserUk
in thread Overlapping portions of sub strings
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |