Output: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
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.ge - [10,30] 0.52 so - [10,20] 0.08 so - [30,40] 1.55
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 DeltronicsIn reply to Re: Count in intervals
by CountZero
in thread Count in intervals
by rkk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |