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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |