Perl - determining the intersection of several numeric ranges

I would like to be able to load long list of positive integer ranges and create a new "summary" range list that is the union of the intersections of each pairs of ranges. And, I want to do this in Perl. For example:

Sample ranges: (1..30) (45..90) (15..34) (92..100)

Intersection of ranges: (15..30) 

The only way I could think of was using a bunch of nested if statements to determine the starting point of sample A, sample B, sample C, etc. and figure out the overlap this way, but it's not possible to do that with hundreds of sample, each containing numerous ranges.

Any suggestions are appreciated!

Answers


The first thing you should do when you need to do some thing is take a look at CPAN to see what tools are available of if someone has solved your problem for you already.

Set::IntSpan and Set::IntRange are on the first page of results for "set" on CPAN.


What you want is the union of the intersection of each pair of ranges, so the algorithm is as follows:

  1. Create an empty result set.
  2. Create a set for each range.
  3. For each set in the list,
    1. For each later set in the list,
      1. Find the intersection of those two sets.
      2. Find the union of the result set and this intersection. This is the new result set.
  4. Enumerate the elements of the result set.

Need Your Help

Prevent TouchKeyboard from grabbing the focus

delphi keyboard focus virtual

I've written a little app in Delphi for a Tablet PC. So there's no keyboard. And there's a small form in the application where the user can enter driver names. I wanted to put a TouchKeyboard on th...

I'm trying to get the name of a server in Weblogic to use in my log4j Config

java log4j java-ee-6 weblogic11g

I'm using log4j in a project that is being deployed to a clustered Weblogic 11g environment, and I'm trying to get the name of the server that has the app deployed on it to use for logs. I seem to

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.