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


In reply to Re^3: multi column multi file comparison by CountZero
in thread multi column multi file comparison 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.