Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I refactored your script into one that illustrates what's going on a bit more clearly. It also runs under use strict and use warnings. It separates the operations into three functions named make_sets, make_ranges and print_ranges. Feedback is welcome.

use strict; use warnings; print_ranges(make_ranges(make_sets(<DATA>))); exit 0; sub make_sets { my @lists = @_; my %set_by; # Hash of sets of integers by labels for my $list (@lists) { while ($list =~ m/(\w+)\s*:\s*(\d+)(?:\s*-\s*(\d+))?/ag) { my $label = $1; my $start = $2; my $end = $+; for my $integer ($start .. $end) { $set_by{$label}{$integer} = 1; } } } return \%set_by; } sub make_ranges { my $set_by = shift; my %ranges_by; # Hash of arrays of integer ranges by labels for my $label (keys %$set_by) { my $index = 0; for my $integer (sort { $a <=> $b } keys %{ $set_by->{$label} +}) { if (not exists $set_by->{$label}{$integer - 1}) { $ranges_by{$label}[$index]{START} = $integer; } if (not exists $set_by->{$label}{$integer + 1}) { $ranges_by{$label}[$index++]{END} = $integer; } } } return \%ranges_by; } sub print_ranges { my $ranges_by = shift; my @ranges; for my $label (sort keys %{ $ranges_by }) { for my $range (@{ $ranges_by->{$label} }) { my $start = $range->{START}; my $end = $range->{END}; push @ranges, $start == $end ? "$label:$start" : "$label:$start-$end +"; } } print join(' ', @ranges) . "\n"; } __DATA__ a:10-34 b:9-12 c:12-24 e: 1-9 a:1-8 a: 19-24 b:2-6 a:7-11 d:9-23 e: 12-23 a:35 e : 20 - 28 foo:42

This prints…

    a:1-35 b:2-6 b:9-12 c:12-24 d:9-23 e:1-9 e:12-28 foo:42

Jim


In reply to Re^2: Union of overlapping numeric intervals by Jim
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-25 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found