umasuresh has asked for the wisdom of the Perl Monks concerning the following question:
Below is the code that does this. My question is as follows:-------inp.txt-------- 1 196 1 190 1 200 2 20 3 25 3 19 3 39 4 40 4 41 4 45
-USuse strict; use warnings; my $in_file = 'inp.txt'; open (IN, $in_file)|| die "couldn't open the in_file\n"; my %r; while (<IN>) { chomp; my $line = $_; my @f = split(/\t/, $line); push(@{ $r{$f[0]} }, $f[1]); } close IN; my @scores =(); my $count; my $i; foreach my $key (sort keys %r) { #print "$key\t"; @scores = @{ $r{$key} }; $count = scalar @scores; #print "@scores[0..$count]\n"; my $sum = 0; #for($i=0; $i <=@scores; $i++) for $i (0..$count) { $sum += $scores[$i]; } my $avg = ($sum/$count); print "$key\t$count\t$avg\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Data munging
by ikegami (Patriarch) on Jan 22, 2010 at 00:41 UTC | |
by umasuresh (Hermit) on Jan 22, 2010 at 00:56 UTC | |
by ikegami (Patriarch) on Jan 22, 2010 at 01:01 UTC | |
|
Re: Data munging
by toolic (Bishop) on Jan 22, 2010 at 00:49 UTC | |
|
Re: Data munging
by BrowserUk (Patriarch) on Jan 22, 2010 at 00:51 UTC | |
by umasuresh (Hermit) on Jan 22, 2010 at 17:01 UTC | |
by BrowserUk (Patriarch) on Jan 22, 2010 at 18:19 UTC | |
by umasuresh (Hermit) on Jan 22, 2010 at 20:41 UTC | |
by BrowserUk (Patriarch) on Jan 23, 2010 at 01:50 UTC | |
| |
by umasuresh (Hermit) on Jan 22, 2010 at 19:49 UTC |