in reply to Read in values and average -- Perl for MacOS X

untested 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, '>end_res.log' or die $!; print FH $_, $/ for @all; close FH;

I hope this more or less clear logic helps.

Replies are listed 'Best First'.
Re^2: Read in values and average -- Perl for MacOS X
by briglass (Initiate) on Feb 15, 2005 at 02:19 UTC

    sh1tn-

    Thanks for the code-- 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

      Please, excuse my lack ot attention.
      my @files; my (@l_count, $l_count); my (@f_count, $f_count); my @all; my $res_file = 'end_res.log'; -f $res_file and unlink $res_file; grep{ -f and push @files, $_ }glob '*'; for( @files ){ open FH, $_ or die $!; while( <FH> ){ /(?:\d+\s*\d*)/ or next; push @l_count, $_ for split '\s+', $_; $l_count += $_ for @l_count; push @f_count, ( $l_count / ($#l_count + 1) ); $l_count = 0; @l_count = (); } close FH; $f_count += $_ for @f_count; push @all, ( $f_count / ($#f_count + 1) ); $f_count = 0; @f_count = (); } open FH, ">$res_file" or die $!; print FH $_, $/ for @all; close FH;