#!/usr/bin/perl
#------------------------------------------------------------------------------#
# This does a sum and mean of column 5 of the input.   This  is  the  time  in #
# seconds for the output of "ping -v" (jc's version of ping):                  #
#   16:00:02 Pkt 56 from 1.2.3.102=walta2 0.600 sec.                           #
# It  also  calculates the variance and standard deviation, and prints them at #
# the end.  We need to figure out how to let the user select a field  ($f)  on #
# the command line.  The commented-out lines are for debugging.                #
#------------------------------------------------------------------------------#
if ($f < 1) {$f = 5;}
$lo = 99999999;
$hi = 0;

while (<>) {
	split;
#	($tod, $what, $pkt, $where, $node, $val) = split;
	++$line;
#	print "Line ",$line,": `"; $, = "' `"; print @_; $, = ' '; print "'\n";
#	printf("$#_=%d\n", $#_);
	if ($#_ > $f) {
		++$lines;
		$val = $_[$f];
#		printf("$#_=%d val=%5.2f\n", $#_, $val);
		$datum[$lines] = $val;
		if ($val < $lo) {$lo = $val;}
		if ($val > $hi) {$hi = $val;}
		$total += $val;
		if ( $val < 0.01 ) {
			printf("Line:%4d\tval=%5.2f\ttotal:%8.2f\tmean:%8.2f\t\n",
				$line, $val, $total, $total / $lines);
		}
#	} else {
#		printf("Line %d has fewer than %d fields.\n", $line, $f);
	}
}
$mean = $total / $lines;
for ($i=1; $i <= $lines; $i++) {
	$diff = $datum[$i] - $mean;
	$var += $diff * $diff;
}
$var /= $lines;
printf("Lines: %d Low: %.2f Mean: %.2f High: %.2f Var: %.2f StdDev: %.2f\n", 
	$lines, $lo, $mean, $hi, $var, sqrt($var));
