Hope this helps: #!/usr/bin/perl # Author: Parag Kalra # Date: 19-SEPT-2010
use strict; use warnings; my %mean_values; while(<DATA>){ chomp; my ($key, $value) = split (/\t/,$_); if ( exists $mean_values{$key} ){ push @{$mean_values{$key}}, $value; } else { ${$mean_values{$key}}[0] = $value; } } foreach (keys %mean_values){ my $cnt = @{$mean_values{$_}}; my $sum = 0; foreach(@{$mean_values{$_}}){ $sum = $sum + $_; } my $mean = $sum / $cnt; print "Key:'$_': | Values: '@{$mean_values{$_}} | Mean: $mean'\n"; } __DATA__ NM_003273 1929.19 NM_053056 4662.262 NM_053056 5728.343 NM_024099 4009.705 NM_001085372 685.4568 NR_003098 4700.29 NM_199337 2727.46 NM_018093 477.2938 NM_018093 813.0701 Cheers, Parag On Fri, Sep 17, 2010 at 6:41 AM, anamika : <anamika...@gmail.com> wrote: > Hello, > I have a file like this: > > NM_003273 1929.19 > NM_053056 4662.262 > NM_053056 5728.343 > NM_024099 4009.705 > NM_001085372 685.4568 > NR_003098 4700.29 > NM_199337 2727.46 > NM_018093 477.2938 > NM_018093 813.0701 > > > I want it to be something like: > If the elements in column 1 are same then take the mean of the values (in > column 2) corresponding to these two/or more similar elements. > Thanks in advance. > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/