Here is a one-liner that provides an example of parsing of lists similar to those you provide into individual letter:number ranges:

perl -Mstrict -Mwarnings -le 'my @l = ( q{a:10-34 b:9-12 c:12-24 e: 1- +9}, q{a:1-8 a: 19-24 b:2-6}, q{a:7-11 d:9-23 e:12-23} ); foreach my $ +m ( @l ) { foreach my $n ( $m =~ m/([a-z]:\s*\d+(?:\s*-\s*\d+))/ig ) +{ $n =~ s/\s+//g; print $n; } }'

Here is a one-liner that provides an example of parsing a key and value or range to get the key, starting and ending values (or repeats the starting value as the ending value, if there is only one value present:

perl -Mstrict -Mwarnings -le 'my $str = q{a:1}; my %f; $str =~ m/([a-z +]):(\d+)(?:-(\d))?/; my ( $k, $starting, $ending ) = ( $1, $2, define +d $3 ? $3 : $2 ); print $k; print $starting; print $ending;'

Here is a one-liner that provides an example of filling in a hash with values in a range (the Data::Dumper code present only to verify that the values were set:

perl -Mstrict -Mwarnings -MData::Dumper -le '$Data::Dumper::Sortkeys = + 1; my %range = ( start => 1, end => 9, ); my %f; foreach my $i ( $ra +nge{start} .. $range{end} ) { $f{$i}++; } print Data::Dumper->Dump( [ + \%f, ], [ qw( *f ) ] );'

Here is a one-liner that takes a hash with values and loops through, to determine the consecutive ranges present:

perl -Mstrict -Mwarnings -le 'my %g = ( 1 => 1, 2 => 2, 3 => 2, 4 => 2 +, 6 => 1, 7 => 1, 8 => 1, 12 => 1, ); my @h = sort { $a <=> $b } keys + %g; my $sv; my $ev; while ( scalar @h ) { my $v = shift @h; if ( ! d +efined $sv ) { $sv = $v; } if ( ! defined $ev ) { $ev = $v; } my $i = + 1; while ( scalar @h and $h[0] == $sv + $i ) { $i++; $ev = shift @h; + } if ( $sv == $ev ) { print $sv; } else { print $sv, q{ - }, $ev; } +$sv = undef; $ev = undef; }'

With all of the examples above, there should be enough, with the addition of a small amount of code, to do what you desire. If not, then you might want to refocus your study of perl on the areas you still find yourself weakest in.

Please comment in the thread if you have any questions.

Hope that helps.


In reply to Re: Union of overlapping numeric intervals by atcroft
in thread Union of overlapping numeric intervals by onlyIDleft

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.