#!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=>'Attitudes towards fruits and vegetables'); print ""; #_________ # Pass=1 #_________ if($pass == 1){ # find the participant mean $yourmean= &averageScores($v1,$v2,$v3,$v4); # save the data to a text file open(INFO, ">>$ENV{'DOCUMENT_ROOT_OLD'}/www/relationshipsresearch.com/P593/sub5.txt"); print INFO "$v1,$v2,$v3,$v4,$yourmean"; print INFO "endline\n"; close (INFO); # find the sample mean for fruit (variable for each person is the second (so 1st) element in data row) &AVGandSD('sub5.txt',4); # provide feedback print ""; print ""; print ""; print ""; print ""; } # End Pass=1 #_________ # Pass=0 #_________ if($pass == 0){ print ""; print ""; print "
"; print ""; print "
Thank you for participating in the study. Have a nice day.


"; print ""; print "Your mean attitude toward vegetables is $yourmean.

"; print "The sample mean attitude toward fruits and vegetables is $Mean and the standard deviation is $SD.
"; print ""; print ""; print ""; &passHidden(); print ""; } # End Pass=0 print $query->end_html; # -----------Fraley Subroutines ----------------------------- # for a good tutorial/overview of Perl subroutines, see # http://www.comp.leeds.ac.uk/Perl/subroutines.html #------------------------------------------------------------ sub averageScores { # sub for averaging scores and adjusting denom for missing cases # fraley march 26 2005 # returns mean, takes as param an array or seq of values # returns zero if no valid cases $nSUB = @_; $validnSUB = 0; $sumSUB = 0; for($iSUB=0;$iSUB<=($nSUB-1);++$iSUB){ if($_[$iSUB] > 0){ $sumSUB = $sumSUB + $_[$iSUB]; $validnSUB = $validnSUB + 1; } } $yourmean = 0; if($validnSUB > 0){ $yourmean = $sumSUB/$validnSUB; } return $yourmean; } # end sub #------------------------------------------------------------------------------- sub passHidden { # This subroutine passes along all information (collected or not) in the form of hidden tags. This allows the information to be carried from one page to the next, despite the fact that the relationship between the subject and the server is "stateless." Instead of writing this out separately for each section of the code in which an item/stimulus is presented, we place the code in a subroutine and call this subroutine at each step along the way. # To customize: Create a hidden tag for each response in question. In the example, we have reponses to 5 self-esteem questions and some demographic items. # Note: An array cannot be passed forward via a hidden tag using the common sense way of HTML. Instead, you must use a cgi.pm expression # Will NOT work: print ""; # Will work: print $query->hidden('varlistRandom',@varlistRandom); print ""; print ""; print ""; print ""; print ""; } # Marie Subroutines---------------------------------------------------------- sub RadioButtons { # Store the questionnaire items as separate elements of an array @items = ("I like vegetables.","I like fruit.","Eating a plate of carrots and brussel sprouts would make me happy.","An apple a day keeps the doctor away."); # One way to do it @variables = ("v1","v2","v3","v4"); # Loop through items 1 through 4 for($i = 0; $i <= 3; ++$i){ # create a new variable for the question number $itemNumber = $i+1; print "$itemNumber. $items[$i]
"; print "Strongly Disagree"; print ""; print ""; print ""; print ""; print ""; print "Strongly Agree

"; } } #------------------------------------------------------------------- sub AVGandSD { # The first [0th] element, $filename, will be the name of the data file (e.g., self-esteem.txt) # The second [1st] element, $index, will be a number indicating which variable number (i.e., column number in a data matrix, with 0 being the first) should be used to find the average and SD $filename = $_[0]; $index = $_[1]; $thisSum= 0; $thisN= 0; $SumSquaredDif= 0; $fullfilename = "$ENV{'DOCUMENT_ROOT_OLD'}/www/relationshipsresearch.com/P593/" . $filename; open(INFO, $fullfilename); @data = ; close(INFO); foreach $key (@data) { @a=split(/,/,$key); if($a[$index] > 0){ $thisSum= $thisSum + $a[$index]; $thisN= $thisN + 1; $thisMean= $thisSum/$thisN; } } foreach $key (@data) { @a=split(/,/,$key); if($a[$index] > 0){ $SumSquaredDif= $SumSquaredDif + $SquaredDif; $Dif= $a[$index] - $thisMean; $SquaredDif= $Dif**=2; $thisSD= sqrt($SumSquaredDif/$thisN); } } if($thisN > 0){ $Mean= sprintf("%.2f",$thisSum/$thisN); $SD= sprintf("%.2f", sqrt($SumSquaredDif/$thisN)); } else { $Mean = "Sample average cannot be computed--too few cases"; } return $SD; return $Mean; } #------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
"; print ""; print "
Attitudes Toward Fruit and Vegetables Survey



"; &RadioButtons(); print " "; print "