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. -
#!/usr/bin/perl
-
use strict;
-
use warnings;
-
-
my ($sum1,$number_of_items1,$average1,
-
$sum2,$number_of_items2,$average2,
-
$sum3,$number_of_items3,$average3,
-
$average_round1,$average_round2,$average_round3);
-
-
my ($folder,@files,$name);
-
my (@temp1,@temp2,@temp3);
-
$folder = "/home/r/codes/";
-
opendir(FOLDER,$folder);
-
-
@files = grep {/calc_box_.*.dat$/} readdir(FOLDER) or die "Check for the dat files";
-
#print "@files";
-
-
foreach my $file(@files)
-
{
-
open(FH,"$file") or die "Check the file";
-
$name = $file;
-
$name =~ s/\.dat//g;
-
while(<FH>)
-
{
-
chomp $_;
-
if($_=~/\s(\d.*)\s(\d.*)\s(\d.*)/)
-
{
-
#print "$1\t$2\t$3\n";
-
@temp1=$1;
-
foreach my $num1(@temp1)
-
{
-
$number_of_items1++;
-
$sum1 += $num1;
-
}
-
$average1 = $sum1 / $number_of_items1;
-
-
-
@temp2=$2;
-
foreach my $num2(@temp2)
-
{
-
$number_of_items2++;
-
$sum2 += $num2;
-
}
-
$average2 = $sum2 / $number_of_items2;
-
-
-
@temp3=$3;
-
foreach my $num3(@temp3)
-
{
-
$number_of_items3++;
-
$sum3 += $num3;
-
}
-
$average3 = $sum3 / $number_of_items3;
-
}
-
}
-
$average_round1 = sprintf '%.4f', $average1;
-
$average_round2 = sprintf '%.4f', $average2;
-
$average_round3 = sprintf '%.4f', $average3;
-
print "$name\t$average_round1\t$average_round2\t$average_round3\n";
-
-
}
-
and here is the text file -
94.9362 94.9362 103.74
-
94.6093 94.6093 103.857
-
94.6289 94.6289 103.697
-
Thanks in advance.
Kumar
2 1571
if you are tyring to sum and average each column individually: - #!/usr/bin/perl
-
use strict;
-
use warnings;
-
my ($col1, $col2, $col3);
-
my $folder = "c:/perl_test";
-
-
opendir(FOLDER,$folder) or die "Check for the dat files: $!";
-
my @files = grep {/calc_box_.*.dat$/} readdir(FOLDER);
-
close FOLDER;
-
-
foreach my $file (@files){
-
open(FH, "c:/perl_test/$file") or die "Check the file: $!";
-
$file =~ s/\.dat//;
-
while(<FH>){
-
if ($_=~/([\d.]+)\s([\d.]+)\s([\d.]+)/){
-
$col1 += $1;
-
$col2 += $2;
-
$col3 += $3;
-
}
-
}
-
my $total = $.;
-
close FH;
-
-
my $average_round1 = sprintf '%.4f', $col1/$total;
-
my $average_round2 = sprintf '%.4f', $col2/$total;
-
my $average_round3 = sprintf '%.4f', $col3/$total;
-
print "$file\t$average_round1\t$average_round2\t$average_round3\n";
-
}
change the path to the folder back to your path. Ask questions if you need some clarification.
Thanks for the help.
I also modified the program bit and its running fine.
Thanks once again for the help.
Kumar
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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
...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
| |