#!C:/perl/bin/perl.exe
use CGI::Carp qw(fatalsToBrowser);
use CGI;
$query = new CGI;
$pass= $query->param('pass');
$v1= $query->param('v1');
$v2= $query->param('v2');
$v3= $query->param('v3');
$v4= $query->param('v4');
if($pass < 1){
$pass = 0;
}
print $query->header;
print $query->start_html(-title=>'Subroutines');
print "";
if($pass == 1){
print "The input values were = $v1, $v2, $v3, and $v4
";
$average = &averageScores($v1,$v2,$v3,$v4);
print "
The average of those values is $average.";
}
if($pass == 0){
print "
";
}
print $query->end_html;
# ----------------- Subroutines -----------------------------
# for a good tutorial/overview of Perl subroutines, see
# http://www.comp.leeds.ac.uk/Perl/subroutines.html
#------------------------------------------------------------
sub averageScores {
# Function for averaging scores and adjusting denom for missing cases
# R. Chris Fraley March 26 2005; modified March 2011
# Returns the mean, takes as param an array or seq of values
# Returns zero if no valid cases
# defined = http://www.perl.com/doc/manual/html/pod/perlfunc/defined.html
$nSUB = @_;
$validnSUB = 0;
$sumSUB = 0;
for($iSUB=0;$iSUB<=($nSUB-1);++$iSUB){
print "i = $iSUB, $_[$iSUB], "; # provide index values for internal testing
if($_[$iSUB]){
print " valid value ";
$sumSUB = $sumSUB + $_[$iSUB]; # add the value to the running total
$validnSUB = $validnSUB + 1; # update the demoninator by 1
print "Current index value = $iSUB, Running total = $sumSUB, Running denominator = $validnSUB"; # print some testing feedback
}
print "
";
}
$mean = 0;
if($validnSUB > 0){
$mean = $sumSUB/$validnSUB;
}
return $mean;
}