in reply to multi column multi file comparison

With the use of some modules (Text::CSV::Auto to read in the data and Number::Range to process the ranges), the following works ... in a way.

use Modern::Perl; use Number::Range; use Text::CSV::Auto qw( process_csv ); use Data::Dump qw /dump/; my $debug = 0; my %database; process_csv('./primary.txt', sub { my $row = shift; push @{$database{$row->{name}}}, [$row->{start}, $row->{end}]; } ); say dump(\%database) if $debug; analyse ('./secondary_1.txt'); analyse ('./secondary_2.txt'); sub analyse { my $file = shift; my %data; process_csv($file, sub { my $row = shift; push @{$data{$row->{name}}}, [$row->{start}, $row->{end}]; } ); say dump(\%data) if $debug; for my $name (sort keys %data) { unless ($database{$name}) { say "Unknown name '$name'"; next; } for my $range_ref (@{$database{$name}}) { print "$name: $range_ref->[0] $range_ref->[1] "; my $range = Number::Range->new($range_ref->[0] .. $range_r +ef->[1]); for my $testrange_ref (@{$data{$name}}) { if ($range->inrange(@$testrange_ref)) { print "present $testrange_ref->[0] $testrange_ref- +>[1] "; } else { print "absent 0 0 "; } } print "\n"; } } }

Output:

Alex: 3 44 absent 0 0 absent 0 0 Alex: 124 175 absent 0 0 present 134 155 Barry: 2 44 present 12 24 James: 6 45 absent 0 0 Alex: 3 44 absent 0 0 Alex: 124 175 present 154 174 Drew: 9 43 present 19 54 absent 0 0 James: 6 45 present 29 45

This solution suffers from the same problem you already mentioned: it checks all ranges and finds that some ranges in the secondary files are outside of the ranges in the primary file (see the data for "Alex"). But unless you find a good rule to define which ranges in the secondary files are to be checked against which ranges in the primary files, there is no way to solve this problem.

Oh and BTW, it is "Perl" (the language) or "perl" (the executable), but never ever "PERL". :)

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^2: multi column multi file comparison
by onlyIDleft (Scribe) on May 24, 2011 at 18:15 UTC

    Hi CountZero,

    Thanks for your posting, I think its pretty close to what I was hoping to produce as an output

    Since I've never used such complicated code before, I was not very successful at modifying it for my exact purpose (see below) Could you please help out again with a modification of your Perl script? (not PERL) :)

    It looks like, in the output you generated, you did more than compare 1 primary file to all secondary files, but I cant say for sure

    Could you help out by modifying your code so that the output is in tabular form for ONLY the primary to EACH secondary file comparison, and no reciprocal comparisons of secondary to primary, or one secondary to another secondary file?

    Also, I think in your example output using your script, for Drew and Barry the comparisons for one of the secondary files is missing, only one set of entries is present.. Is that fixed easily?

    In terms of range comparison, it is very simple math, but I think you have that implemented right. Anyways, described below:

    In the original data file, as a rule, ALWAYS p1<p2, and S1-1 < S1-2, and S2-1 < S2-2

    If primary range is numerically p1 to p2 and for secondary ranges they are S1-1 to S1-2 (for 1st secondary file), S2-1 to S2-2 (for 2nd secondary file), then simply

    S1-1 >= p1

    S1-1 <= p2

    S1-2 >= p1

    S1-2 <= p2

    This means the range S1-1 to S1-2 is nested within the range p1 to p2, or can be the same, but any extension past the primary range is disallowed... Likewise for any other range, S2-1 to S2-2 for another file....Therefore, again

    S2-1 >= p1

    S2-1 <= p2

    S2-2 >= p1

    S2-2 <= p2

    Thanks again CountZero

      I see where I went wrong.

      I checked the secondary files against the primary file (analyse subroutine), rather than the other way around. Easy enough to switch that:

      use Modern::Perl; use Number::Range; use Text::CSV::Auto qw( process_csv ); use Data::Dump qw /dump/; my $debug = 0; my %database; process_csv('./primary.txt', sub { my $row = shift; push @{$database{$row->{name}}}, [$row->{start}, $row->{end}]; } ); say dump(\%database) if $debug; analyse ('./secondary_1.txt'); analyse ('./secondary_2.txt'); sub analyse { my $file = shift; say "--Checking $file--"; my %data; process_csv($file, sub { my $row = shift; push @{$data{$row->{name}}}, [$row->{start}, $row->{end}]; } ); say dump(\%data) if $debug; for my $name (sort keys %database) { unless ($data{$name}) { for my $range_ref (@{$database{$name}}) { say "$name: $range_ref->[0] $range_ref->[1] absent 0 +0"; } next; } for my $range_ref (@{$database{$name}}) { print "$name: $range_ref->[0] $range_ref->[1] "; my $range = Number::Range->new($range_ref->[0] .. $range_r +ef->[1]); for my $testrange_ref (@{$data{$name}}) { if ($range->inrange(@$testrange_ref)) { print "present $testrange_ref->[0] $testrange_ref- +>[1] "; } else { print "absent 0 0 "; } } print "\n"; } } say '------------------------------'; }
      Which produces the following output:
      --Checking ./secondary_1.txt-- Alex: 3 44 absent 0 0 absent 0 0 Alex: 124 175 absent 0 0 present 134 155 Barry: 2 44 present 12 24 Drew: 9 43 absent 0 0 James: 6 45 absent 0 0 ------------------------------ --Checking ./secondary_2.txt-- Alex: 3 44 absent 0 0 Alex: 124 175 present 154 174 Barry: 2 44 absent 0 0 Drew: 9 43 present 19 54 absent 0 0 James: 6 45 present 29 45 ------------------------------

      The whole range checking is done thanks to the Number::Range module. Please, check out its documentation and more specifically the inrange-method called in scalar context. I hand it the begin and endpoints of the range to be checked and if both are within the range to be checked against, it returns true, hence the whole range must be within.

      The analyse subroutine in the script above takes as its only argument the filename of a "secondary" file and the primary file is checked against this secondary file. There is no secondary-against-secondary checking done or a reciprocal secondary against primary. Check the main for-loop inside this subroutine: it uses the data in the %database-hash which has been populated with the data from the primary file.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        Dear CountZero,

        Thanks for the modifications you've made to your script, and so promptly, I am quite thankful to you

        The one thing I suppose that is still a little different from the output I'd first described is that for each row of the primary input file, I want in the output, additionally, ONLY 3 additional columns for EACH secondary input file.

        So if there are 3 secondary files, that would be 3*3=9 additional columns added per every row of the primary file. If there are only 2 secondary files, add 2*3=6 additional columns per every row of the primary file. This will make a grid rather than a running list. The grid is way easier to use, and also compatible with MS Excel that some other folks at work use...

        The one thing that therefore has to be changed in this script is to quote 'absent 0 0' JUST ONCE if all instances in the secondary files are absent. And similarly, quote ONLY 'present $start $end' when there is a range match, and not to worry about including absent 0 0 IF there is a match within primary range

        To make that rather abstract explanation above easier to understand, let me take your output example and change it to what I am hoping to get as the final output:

        --Checking primary.txt to ./secondary_1.txt and .secondary_2.txt--

        Alex: 3 44 absent 0 0 absent 0 0

        Alex: 124 175 present 134 155 present 154 174

        Barry: 2 44 present 12 24 absent 0 0

        Drew: 9 43 absent 0 0 present 19 54

        James: 6 45 absent 0 0 present 29 45

        I think I did a pretty average job of describing the output in previous postings. Does this sort of make sense now?

        Thanks in advance, once again, CountZero, and I will now look into learning the modules that you have referred me to in your earlier postings.