473,320 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Average from list of files

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
2 1571
KevinADC
4,059 Expert 2GB
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
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

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

Similar topics

0
by: Sara | last post by:
I am trying to find the average number of products of the set that is on rows...dynamically. For example, if I had states on rows and was looking at the average number of products, I would see...
1
by: David Franit | last post by:
Hello, I am looking at tuning NUM_POOLAGENTS for many v7.x and v8.x UDB instances. Aside from logging into each instance and running 'db2 list applications', or writing a script to do so, is...
4
by: sbowman | last post by:
I have a table with help desk ticketing information. There is a Date/Time open, Date/Time closed field both formatted as: MM/DD/YYYY hh:nn:ss I need to calculate the difference between these two...
1
by: HEMH6 | last post by:
Weihted Average Write a C program to calculate and print the weighted average of a list of N floating point number, using the formula Xave = F1X1 + F2X2+...+ FnXn where the F's are...
2
by: kumarboston | last post by:
hi all, i am processing a certain number of files from a directory and each file has around 5 columns and 200 rows with tab separated values. I am trying to grab each file and calculate the average...
21
by: Bill Cunningham | last post by:
I have create these 2 files. Called main.c and atr.c. They seem to work pretty well. I just wanted to submit them to see what if any errors others that know more might find. Thanks. atr.c ...
5
by: p3rk3le | last post by:
So, I'm about to do a sequential search on a table (n contents) of random numbers. I have to print the average between the number of comparisons and the contents of the table (n) and the...
2
by: jac130 | last post by:
I have an array, populated with student grades. there is a list of student names, but their grades are stored in a class level array. there is a calculate average button,that is supposed to calculate...
5
by: kumarboston | last post by:
Hi all, I was trying to calculate the average value from different parts of the same data file. For example, if suppose we have number 1 - 10 and i was trying to calculate the average of only first...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.