#------------------------------------------------------------------------- # Reverse keying (MAX + MIN - X) #------------------------------------------------------------------------- $esteem = ($v01 + $v02 + (6-$v03) + $v04 + (6-$v05))/5; $esteem2 = sprintf("%.2f",$esteem); # other maths listed on p. 109 #------------------------------------------------------------------------- # if condition to customize the feedback #------------------------------------------------------------------------- if($esteem2 > 3){ print "Your self-esteem score is above the midpoint of the scale.
"; } if($esteem2 < 3){ print "Your self-esteem score is below the midpoint of the scale.
"; } # other conditional tests listed on pp. 120-121 for numbers and letter strings #------------------------------------------------------------------------- # variables vs. arrays #------------------------------------------------------------------------- $v01 = 4; print "variable = $v01
"; $v01 = "Secure attachment"; print "variable = $v01
"; @v01 = (1,4,3,8,5); print "element 0 = $v01[0]
"; print "element 1 = $v01[1]
"; print "element 2 = $v01[2]
"; print "element 3 = $v01[3]
"; print "element 4 = $v01[4]
"; #------------------------------------------------------------------------- # foreach loop demonstration 1 # Find average score for other folks #------------------------------------------------------------------------- # $date1,$date2,$v01,$v02,$v03,$v04,$v05,$esteem2,endline\n # 0 1 2 3 4 5 6 7 8 $sum= 0; $n= 0; open(INFO, "$ENV{'DOCUMENT_ROOT'}/P595/form3.txt"); @data = ; close(INFO); foreach $key (@data) { @a = split(/,/,$key); $sum= $sum + $a[10]; $n= $n + 1; } $esteemmean= $sum/$n; $esteemmean = sprintf("%.2f", $esteemmean); # The foreach command is used to construct a loop that cycles through each line of data contained in the array, @data. $key is a temporary label used to reference each line of data in the array as it is processed within the loop. The precise way in which each line of @data is processed is specificed within the braces. # # The generic form of foreach is # # foreach $key (@data) { # # } # # # The split command is used to specify the way elements within a row will be partitioned into discrete variables. # # The generic form of split is # # split(/,/,$key); # # where /,/ represents the delimiter you wish to use to separate the items (in this case, a comma) #------------------------------------------------------------------------- # foreach loop demonstration 2 #------------------------------------------------------------------------- open(INFO, "$ENV{'DOCUMENT_ROOT'}/P593/form3.txt"); @data = ; close(INFO); print "Thank you for your participation. Your self-esteem score is $esteem2 on a scale ranging from 1 (low) to 5 (high).

"; print "The table below summarizes the self-esteem scores for other people in the sample.

"; print ""; print ""; foreach $key (@data) { @a = split(/,/,$key); print ""; } print "
Date Time Esteem Score
$a[0] $a[1] $a[7]
"; #------------------------------------------------------------------------- # CSS via Perl #------------------------------------------------------------------------- print $query->header; print $query->start_html(-title=>'Thank You'); print ""; #------------------------------------------------------------------------- # Getting fancy : CSS, variables, and loops #------------------------------------------------------------------------- open(INFO, "$ENV{'DOCUMENT_ROOT'}/P593/form3.txt"); @data = ; close(INFO); print "Thank you for your participation. Your self-esteem score is $esteem2 on a scale ranging from 1 (low) to 5 (high).

"; print "The table below summarizes the self-esteem scores for other people in the sample.

"; print "
"; print ""; $rowCount = 0; foreach $key (@data) { @a = split(/,/,$key); if($rowCount == 0){ print ""; $newrowCount = 1; } if($rowCount == 1){ print ""; $newrowCount = 0; } $rowCount = $newrowCount; } print "
Date Time Esteem Score
$a[0] $a[1] $a[7]
$a[0] $a[1] $a[7]
";