Hello rkk,

Assuming FILE2 is not too large, the best strategy is to read FILE2 into a suitable data structure, then read FILE1 line-by-line and add that line’s third-column datum to the appropriate interval count(s). This avoids the need to perform any kind of search on FILE1:

#! perl use strict; use warnings; use Data::Dump; my %intervals = ( so => { "10,20" => 0, "30,40" => 0, }, ge => { "10,30" => 0 }, ); while (my $line = <DATA>) { my ($id, $num, $datum) = split /\s+/, $line; for my $interval (keys %{ $intervals{$id} }) { my ($min, $max) = split /,/, $interval; if ($num >= $min && $num <= $max) { $intervals{$id}->{$interval} += $datum; } } } dd \%intervals; __DATA__ so 10 0.05 so 11 0.03 so 25 0.15 so 35 0.3 so 36 0.25 so 37 1 ge 14 0.12 ge 20 0.4

Output:

12:41 >perl 1037_SoPW.pl { ge => { "10,30" => 0.52 }, so => { "10,20" => 0.08, "30,40" => 1.55 }, } 12:41 >

(Populating %intervals from FILE2 is left as the proverbial exercise for the reader!)

P.S. In future, please post non-site-related questions to Seekers of Perl Wisdom. Perl Monks Discussion is reserved for issues affecting the PerlMonks site itself, not Perl in general.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Count in intervals by Athanasius
in thread Count in intervals by rkk

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.