#!/usr/bin/perl -w # my quiz use strict; use CGI qw(param); my $max; my @question; my @correct; my @answer; my $i = "0"; my $j = "0"; my $k = "0"; my $score = "0"; my (@A, @B, @C, @D); my $who = param("name"); my $surname = param("surname"); open(QSTNS, "questions.txt") || die "cannot open questions.txt $!"; while () { $_ =~ /^(Quiz)(\d)$/; $max = $2; } close(QSTNS) || warn "cannot close questions.txt $!"; while ($k < $max) { push (@answer,param("answer$k")); $k++; } print <Quiz

WELCOME TO MY QUIZ!

Firstbit if (param()) { open(QSTNS, "questions.txt") || die "cannot open questions.txt $!"; while() { if ($_ =~ /^A\d:/) { chomp; s/A\d://; push (@correct,$_); } } close(QSTNS) || warn "cannot close questions.txt $!"; while ($j < $max) { if ($answer[$j] eq $correct[$j]) { $score++; #print "

score: $score"; } #print "

j: $j"; $j++; } #print "

@correct, @answer"; print "

$who $surname, you scored $score"; open(LOGS, ">>log.txt") || die "cannot append log.txt $!"; print LOGS "$who $surname scored $score\n"; close(LOGS); } else { open(QSTNS, "questions.txt") || die "

cannot open questions.txt $!"; while() { if ($_ =~ /^Q\d/) { chomp; push (@question,$_); } if ($_ =~ /^C\d:/) { chomp; s/C\d://; $_ =~ /([^,]+),([^,]+),([^,]+),([^,]+)/; push (@A,$1); push (@B,$2); push (@C,$3); push (@D,$4); } } #print "

A: @A"; close(QSTNS); print <

Please enter your first name:

Please enter your surname: Startform while ($i < $max) { print <$question[$i]
       $A[$i]
       $B[$i]
       $C[$i]
       $D[$i]
Loop $i++; } print < Endform } print < Endbit #### #################################### #The questions.txt file should be formatted as follows ###################################### Quiz5 Q1. What is the capital of Afghanistan? C1:Tirana,Grozny,Kabul,Tblisi A1:Kabul Q2. What is the capital of Albania? C2:Tirana,Grozny,Kabul,Tblisi A2:Tirana Q3. What is the capital of Chechnya? C3:Tirana,Grozny,Kabul,Tblisi A3:Grozny Q4. How many balls on a snooker table at the break off? C4:15,21,22,25 A4:22 Q5. What's the nearest planet to the Sun? C5:Earth,Venus,Mercury,Pluto A5:Mercury