In Perl's spirit of TIMTOWTDI:
use Modern::Perl '2014'; use Number::Interval; use List::Util qw/sum/; my %raw_intervals = ( so => [ [ 10, 20 ], [ 30, 40 ] ], ge => [ [ 10, 30 ], ], ); my @interval_objects; for my $id ( keys %raw_intervals ) { for my $interval ( @{ $raw_intervals{$id} } ) { push @interval_objects, [ $id, Number::Interval->new( IncMax => 1, IncMin => 1, Min => $interval->[0], Max => $interval->[1] ) ]; } } while (<DATA>) { my ( $id, $value, $count ) = split; for my $interval (@interval_objects) { if ( $id eq $interval->[0] and $interval->[1]->contains($value +) ) { push @$interval, $count; last; } } } for my $interval (@interval_objects) { my ( $id, $interval, @values ) = @$interval; say "$id - ", $interval->stringify, ' ', sum(@values); } __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:
ge - [10,30] 0.52 so - [10,20] 0.08 so - [30,40] 1.55
Note that in the array @interval_objects are stored all the values of each line of FILE1 for this particular interval and thereby it becomes trivially easy to calculate minimum value, maximum value, average value, count, standard deviation, ... just by applying another function from List::Utils or any other function which accepts an array as its input.

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

My blog: Imperial Deltronics

In reply to Re: Count in intervals by CountZero
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.