in reply to Re^4: Mean and standard deviation loop
in thread Mean and standard deviation loop
So one way of doing this would be to use a hash that uses an array-ref to collect the data belonging to one partition:
Then you can later process these partitions to calculate e.g. the mean:my %partitions; while ( ... my @cols = split(',', $line); my $pname = "$cols[0]:$cols[2]"; $partitions{$pname} ||= []; push @{$partitions{$pname}}, $cols[3]; }
This assumes that the data you want to process are in your "Col 4" only. If you need to include the other columns, simply push them also onto the array.use List::Util qw(sum); for my $pname (sort keys %partitions) { my $count = scalar @{$partition{$pname}}; my $sum = sum @{$partition{$pname}}; my $mean = $sum/$count; # std dev is left as an exercise }
BEWARE: Untested code!
|
|---|