At 18:16 -0800 07/01/2011, S.F. wrote:

I have a data file with n columns and r row.
The first 3 columns and the first 5 rows are:

2 3 1
1 6 X
4 0 X
X 8 X
5 X 5

The "X" means missing.
How could I write a script to calculate the average by column and
replace "X" with the average?

There must be a cleverer way to do it but this works:


#!/usr/local/bin/perl
use strict;
my (@col1, @col2, @col3);
my ($tot1, $tot2, $tot3, $avg1, $avg2, $avg3);
my ($n, $output);
while (<DATA>){
  chomp;
  my ($v1, $v2, $v3) = split /\s*/;
  push @col1, $v1; push @col2, $v2; push @col3, $v3;
}
#
$n=0; for (@col1) {$n-- if /x/i ; $tot1 += $_}
$avg1 = $tot1/($n + @col1);
$n=0; for (@col2) {$n-- if /x/i ; $tot2 += $_}
$avg2 = $tot2/($n + @col1);
$n=0; for (@col3) {$n-- if /x/i ; $tot3 += $_}
$avg3 = $tot3/($n + @col1);
#
for $n (0...@col1-1){
  $_ = $col1[$n]; s~x~$avg1~i;
  $output .= "$_\t";
  $_ = $col2[$n]; s~x~$avg2~i;
  $output .= "$_\t";
  $_ = $col3[$n]; s~x~$avg3~i;
  $output .= "$_\n";
}
print $output;
__DATA__
2 3 1
1 6 X
4 0 X
X 8 X
5 X 5




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to