Michael Shipper > Data-Range-Compare-1.030 > Data::Range::Compare

Download:
Data-Range-Compare-1.030.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
View/Report Bugs
Module Version: 1.030   Source  

NAME ^

Data::Range::Compare - Find gaps & intersections in lists of ranges

SYNOPSIS ^

  use Data::Range::Compare qw(HELPER_CB);

  my %helper=HELPER_CB;

  my @tom;
  my @harry;
  my @sally;

  push @tom,Data::Range::Compare->new(\%helper,0,1);
  push @tom,Data::Range::Compare->new(\%helper,3,7);

  push @harry,Data::Range::Compare->new(\%helper,9,11);

  push @sally,Data::Range::Compare->new(\%helper,6,7);

  my @cmp=(\@tom,\@harry,\@sally);

  my $sub=Data::Range::Compare->range_compare(\%helper,\@cmp);
  while(my @row=$sub->()) {
    my $common_range=
      Data::Range::Compare->get_common_range(\%helper,\@row);
    print "Common Range: $common_range\n";
    my ($tom,$harry,$sally)=@row;
    print "tom:   $tom\n";
    print "harry: $harry\n";
    print "sally: $sally\n";
  }

DESCRIPTION ^

This package provides a universal framework for calculating the intersections and gaps in/of 2 dimensional ranges.

Getting Started

In order to use this package on your data, you will need to create 3 subroutines to handle computations of your data.

Putting it all together in %helper

%helper is the primary driver for this framework.

Data::Range::Compare relies on %helper to do its internal work. So this section explains how to populate %helper.

  # to import the default %helper subroutines into your package
  use Data::Range::Compare qw(:HELPER);

  my %helper=(
    add_one=>\&add_one
    sub_one=>\&sub_one
    cmp_values=>\&cmp_values
  );

Comparing Range Intersections

In order to compare lists of ranges for intersections, you will need to create a list of lists containing Data::Range::Compare Objects.

Example:

  my %helper=HELPER_CB;

  my @list_a=(
    Data::Range::Compare->new(\%helper,1,2)
    ,Data::Range::Compare->new(\%helper,2,3)
  );
  my @list_b=(
    Data::Range::Compare->new(\%helper,0,1)
    ,Data::Range::Compare->new(\%helper,4,7)
  );

  my @compaire_all=(\@list_a,\@list_b);

Once the list of list references has been created it is possible to compare the list of ranges.

Example:

  my $cmp=Data::Range::Compare->range_compare(
      \%helper
      ,\@compaire_all
  );

The above creates an anonymous subroutine that can iterate through the list of ranges.

Example:

  while(my ($column_a,$column_b) = $cmp->() ) {
    my $common=Data::Range::Compare->get_common_range(
      \%helper
        ,[
          $column_a
          ,$column_b
        ]
    );

    print "\nCommon Range: ",$common,"\n";
    if($column_a->missing) {
      print "Not in set a";
    } else {
      print $column_a
    }
    print " , ";
    if($column_b->missing) {
      print "Not in set b";
    } else {
      print $column_b
    }
    print "\n";
  }

Output:

  Common Range: 0 - 0
  Not in set a , 0 - 1

  Common Range: 1 - 1
  1 - 3 , 0 - 1

  Common Range: 2 - 3
  1 - 3 , Not in set b

  Common Range: 4 - 7
  Not in set a , 4 - 7

In the loop we check to see if any of our ranges were found in our sets of data. In this case the following ranges were not shared in both sets of data.

  0 - 0
  2 - 3
  4 - 7 

Our only intersecting range was

  1 - 1

The anonymous subroutine does not actually skip over ranges that were not in both sets of data. Instead it creates a new missing range that represents the gap from that set of data.

If we just wish to see the intersecting sets of data we need to change our while loop just a little bit.

  while(my ($column_a,$column_b) = $cmp->() ) {
    next if $column_a->missing;
    next if $column_b->missing;
    my $common=Data::Range::Compare->get_common_range(
      \%helper
        ,[
          $column_a
          ,$column_b
        ]
    );
    print "Column_a and Column_b intersect at: ",$common,"\n";
  }

  Output:

  Column_a and Column_b intersect at: 1 - 1

As noted when looking at our data, the only intersecting range was "1 - 1" between @list_a and @list_b.

OO Methods

This section covers the OO Methods in the package.

Sort Methods

This section documents the export-able sort subroutines. These are low level sort subroutines and must be used in a call to "sort".

Example:

  @list=sort sort_in_presentation_order @list;

Export list

:KEYS

  key_helper
  key_start
  key_end
  key_generated
  key_missing
  key_data

:HELPER_CB

  HELPER_CB

:HELPER

  add_one
  sub_one
  cmp_values

:SORT

  sort_largest_range_end_first
  sort_largest_range_start_first
  sort_smallest_range_start_first
  sort_smallest_range_end_first
  sort_in_consolidate_order
  sort_in_presentation_order

SEE ALSO ^

Data::Range::Compare::Cookbook

AUTHOR ^

Michael Shipper

Source-Forge Project ^

As of version 1.026 the Project has been moved to Source-Forge.net

Data Range Compare https://sourceforge.net/projects/data-range-comp/

COPYRIGHT ^

Copyright 2010 Michael Shipper. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: