brianwustl
Programmer
I would like to open a text file that contains hundreds of lines of tab delimited numerical values (hundreds of values per line) between 0 and 255. I would like to take the mean average of all these values (so I guess the program would add up all the values and then divide them by the number of values per file). So the result for each text file would be a single value between 0 and 255.
I would like to perform this on a series of files and then create a single text file that holds all of these mean values, each on a new line:
i.e.,
230
198
194
6
201
57
115
...etc.
Also, this would need to be run with the perl included with Mac OS X.
Here is what I have so far:
It seems to work perfectly, except for the fact that the resulting mean value seems to be (sum * count) instead of (sum / count). I tried fiddling with the code to get it to output the ratio instead of the product, but to no avail.
For example, a file with the following comma delimited values:
1 1 1
outputs:
9
(1+1+1 * 3)
Any ideas?
Thanks,
Brian
I would like to perform this on a series of files and then create a single text file that holds all of these mean values, each on a new line:
i.e.,
230
198
194
6
201
57
115
...etc.
Also, this would need to be run with the perl included with Mac OS X.
Here is what I have so far:
Code:
my @files;
my (@l_count, $l_count);
my (@f_count, $f_count);
my @all;
grep{ -f and push @files, $_ }glob '*';
for( @files ){
open FH, $_ or die $!;
while( <FH> ){
push @l_count, $_ for split '\s+', $_;
for( @l_count ){
$l_count += $_ for @l_count
}
@l_count = ();
push @f_count, ( $l_count / $#l_count );
$l_count = 0;
}
close FH;
$f_count += $_ for @f_count;
@f_count = ();
push @all, ( $f_count / $#f_count );
$f_count = 0;
}
open FH, '>output/end_res.log' or die $!;
print FH $_, $/ for @all;
close FH;
It seems to work perfectly, except for the fact that the resulting mean value seems to be (sum * count) instead of (sum / count). I tried fiddling with the code to get it to output the ratio instead of the product, but to no avail.
For example, a file with the following comma delimited values:
1 1 1
outputs:
9
(1+1+1 * 3)
Any ideas?
Thanks,
Brian