#!/usr/local/bin/perl

# program accepts a file with a single column of
# numbers, then reports the number of data entries,
# the mean, standard deviation, and median values.
$, = ' ';		# set output field separator
$\ = "\n";		# set output record separator

$error_count = 0;

while (<>) {
    ($Fld1) = split(' ', $_, 9999);

    if ($Fld1 ne 'failed') {
	    push(@X, $Fld1);
    }
    else {
	print "got error\n";
	push(@error, $.);
	$error_count++;
    }
}

$N = $#X + 1;
# put samples in order of increasing value:

@X = sort { $a <=> $b} @X;

# median is central sample, for odd number of samples,
# and average of central 2 samples for even number of samples.

if (($N / 2 - int($N / 2)) > 0) {
    $median = $X[int($N / 2)];
} else {
    $median = ($X[($N / 2) - 1] + $X[($N / 2) + 1]) / 2;
}

$sum = 0;
for ($j = 0; $j < $N; $j++) {
    $sum = $sum + $X[$j];
}
$ave = $sum / $N;

$sum = 0;
for ($j = 0; $j < $N; $j++) {
    $sum = $sum + ($ave - $X[$j]) * ($ave - $X[$j]);
}
# Note:  the following might want to be (N-1), for small sample size 
$stddev = sqrt($sum / $N);
printf "number of samples = %d\n", $N;
printf "minimum = %g\n", $X[0];
printf "maximum = %g\n", $X[$#X];
printf "mean    = %g\n", $ave;
printf "std_dev = %g\n", $stddev;
printf "median  = %g\n", $median;

if ($error_count > 0) {
    for ($i = 0; $i < $error_count; $i++) {
	print 'error @ trial ' . $error[$i];
    }
}

