#!C:/perl/bin/perl.exe
use CGI;
$query = new CGI;
#
# Read in all ten scores from CGI packet (even if those items were not used)
#
$v01= $query->param('v01');
$v02= $query->param('v02');
$v03= $query->param('v03');
$v04= $query->param('v04');
$v05= $query->param('v05');
$v06= $query->param('v06');
$v07= $query->param('v07');
$v08= $query->param('v08');
$v09= $query->param('v09');
$v10= $query->param('v10');
#
# We're going to use the unrandomize array, @varlist, to store the names of the scores and the array @reversekey
# to represent which items will need to be reverse keyed
#
@varlist= ("v01", "v02", "v03", "v04", "v05", "v06", "v07", "v08", "v09", "v10");
@reversekey = (0,0,1,0,1,0,0,1,1,1);
print $query->header;
print $query->start_html(-title=>'Thank You');
# In this loop, we cycle through each item that could have been administered.
# For each item, we determine whether the item was administered by asking whether
# the response is between 1 and 5. (If it is not, then the item was not administered, or, at least, not responded to.)
# If the response exists, we add the response to our sum called $esteem. Notice that we only add the response
# for non-reverse keyed items. If the item is reverse keyed (i.e., if "if(reversekey[$i] == 1"), we subtract
# 6 from the response before adding it to $esteem. This process allows us to create a sum of all the responses
# for the items administered.
print "Note: This is the 'results' page for the challenge posed at the end of Chapter 7. I have printed the 'internal computations' to the screen (see below) so that you can get a better sense of how the program is working. I've also included detailed comments in the code itself.--Chris
";
$esteem = 0;
for($i = 0; $i <= 9; ++$i){
$response= $varlist[$i];
print "$varlist[$i] : $$response : ";
if($$response >= 1 && $$response <= 5){
if($reversekey[$i] == 0){
$esteem = $esteem + $$response;
print "$esteem ";
}
if($reversekey[$i] == 1){
$esteem = $esteem + (6 - $$response);
print "$esteem ";
}
}
print "
";
}
$esteem = $esteem/5;
$esteem2 = sprintf("%.2f", $esteem);
# The following commands simply save the data to a text file. There is nothing new here. It should be noted, however, that, for ease of programming, I am saving all of the item responses regardless of whether those items were used. The "empty" items will simply appear as blanks in the dataset.
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$ip= $query->remote_addr();
# save the data to a file called self-esteemextra.text
open(INFO, ">>$ENV{'DOCUMENT_ROOT'}/data/self-esteemextra.txt");
flock (INFO, 2);
print INFO "$mon/$mday/$year\, ";
print INFO "$hour:$min:$sec\, ";
print INFO "$ip, ";
foreach $key (sort($query->param))
{
$value = $query->param($key);
print INFO "$value, ";
}
print INFO "$esteem2, ";
print INFO "endline \n";
close (INFO);
print "
Self-Esteem Results Page
Your self-esteem score is $esteem2, on a scale ranging from 1 to 5.
";
print $query->end_html;