By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,851 Members | 1,410 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,851 IT Pros & Developers. It's quick & easy.

Average from list of files

P: 55
Hi All,
I have some 10 text files with 3 columns of number in them, I am tring to calculate the average of each column and printing a file with file name tab separeted by average values of 3 columns.
Here is my code, for the first file the program gives me correct values but after that wrong, someone could suggest me where I am wrong.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. my ($sum1,$number_of_items1,$average1,
  6.     $sum2,$number_of_items2,$average2,
  7.     $sum3,$number_of_items3,$average3,
  8.     $average_round1,$average_round2,$average_round3);
  9.  
  10. my ($folder,@files,$name);
  11. my (@temp1,@temp2,@temp3);   
  12. $folder = "/home/r/codes/";
  13. opendir(FOLDER,$folder);
  14.  
  15. @files = grep {/calc_box_.*.dat$/} readdir(FOLDER) or die "Check for the dat files";
  16. #print "@files";
  17.  
  18. foreach my $file(@files)
  19. {                  
  20.     open(FH,"$file") or die "Check the file";
  21.     $name = $file;
  22.     $name =~ s/\.dat//g;
  23.     while(<FH>)
  24.     {
  25.         chomp $_;
  26.         if($_=~/\s(\d.*)\s(\d.*)\s(\d.*)/)
  27.         {
  28.             #print "$1\t$2\t$3\n";
  29.             @temp1=$1;
  30.             foreach my $num1(@temp1)
  31.             {
  32.                 $number_of_items1++;
  33.                 $sum1 += $num1;
  34.             }
  35.             $average1 = $sum1 / $number_of_items1;
  36.  
  37.  
  38.             @temp2=$2;
  39.             foreach my $num2(@temp2)
  40.             {
  41.                 $number_of_items2++;
  42.                 $sum2 += $num2;
  43.             }
  44.             $average2 = $sum2 / $number_of_items2;
  45.  
  46.  
  47.             @temp3=$3;
  48.             foreach my $num3(@temp3)
  49.             {
  50.                 $number_of_items3++;
  51.                 $sum3 += $num3;
  52.             }
  53.             $average3 = $sum3 / $number_of_items3;    
  54.         }
  55.     }
  56.     $average_round1 = sprintf '%.4f', $average1;
  57.     $average_round2 = sprintf '%.4f', $average2;
  58.     $average_round3 = sprintf '%.4f', $average3;
  59.     print "$name\t$average_round1\t$average_round2\t$average_round3\n";
  60.  
  61. }
  62.  
and here is the text file

Expand|Select|Wrap|Line Numbers
  1.  94.9362 94.9362 103.74
  2. 94.6093 94.6093 103.857
  3. 94.6289 94.6289 103.697
  4.  
Thanks in advance.

Kumar
Jan 22 '08 #1
Share this Question
Share on Google+
2 Replies


KevinADC
Expert 2.5K+
P: 4,059
if you are tyring to sum and average each column individually:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my ($col1, $col2, $col3);
  5. my $folder = "c:/perl_test";
  6.  
  7. opendir(FOLDER,$folder) or die "Check for the dat files: $!";
  8. my @files = grep {/calc_box_.*.dat$/} readdir(FOLDER);
  9. close FOLDER;
  10.  
  11. foreach my $file (@files){                  
  12.    open(FH, "c:/perl_test/$file") or die "Check the file: $!";
  13.    $file =~ s/\.dat//;
  14.    while(<FH>){
  15.       if ($_=~/([\d.]+)\s([\d.]+)\s([\d.]+)/){
  16.          $col1 += $1;
  17.          $col2 += $2;
  18.          $col3 += $3;
  19.       }
  20.    }
  21.    my $total = $.;
  22.    close FH;
  23.  
  24.    my $average_round1 = sprintf '%.4f', $col1/$total;
  25.    my $average_round2 = sprintf '%.4f', $col2/$total;
  26.    my $average_round3 = sprintf '%.4f', $col3/$total;
  27.    print "$file\t$average_round1\t$average_round2\t$average_round3\n";
  28. }
change the path to the folder back to your path. Ask questions if you need some clarification.
Jan 23 '08 #2

P: 55
Thanks for the help.

I also modified the program bit and its running fine.

Thanks once again for the help.

Kumar
Jan 23 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.