
sessmurda
Novice
Dec 13, 2010, 3:54 PM
Post #3 of 5
(1504 views)
|
|
Re: [rovf] Average second column of a file for those with same value in first column
[In reply to]
|
Can't Post
|
|
Thanks for the strategy. So I guess my main issue is I am unsure of how to calculate the average. My code below is telling me that it is dividing by 0 at line 3310867, which has a normal value like any other.
use Data::Dumper; use List::Util qw/sum/; sub avg { sum(@_) / @_ } open (IN, "$ARGV[0]") || die "nope\n"; my %hash; # Read data line by line while (<IN>) { chomp; my $line = $_; my $key = (split/\t/, $line)[0]; push @{ $hash{$key} }, $line; } my $avg = avg @{ $hash{$key} }; print $avg,"\n"; close IN; I used data dumper before calculating the average to be sure that it loads correctly, which it does. Any suggestions? Thanks
|