This is my first posting so go easy! Basically it reads a text file for questions,choice of answers, and correct answers. Displayed in a browser the quiz will then count responses and give a score whilst logging details to a file. It's version1, not well commented and I will be posting version2 soon. I just couldn't wait to share it!
#!/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 (<QSTNS>) { $_ =~ /^(Quiz)(\d)$/; $max = $2; } close(QSTNS) || warn "cannot close questions.txt $!"; while ($k < $max) { push (@answer,param("answer$k")); $k++; } print <<Firstbit; Content-type: text/html <HTML><HEAD><TITLE>Quiz</TITLE></HEAD> <BODY><H1>WELCOME TO MY QUIZ!</H1> Firstbit if (param()) { open(QSTNS, "questions.txt") || die "cannot open questions.txt $!" +; while(<QSTNS>) { 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 "<P>score: $score"; } #print "<P>j: $j"; $j++; } #print "<P>@correct, @answer"; print "<p>$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 "<P>cannot open questions.txt +$!"; while(<QSTNS>) { 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 "<P>A: @A"; close(QSTNS); print <<Startform; <FORM> <P>Please enter your first name: <INPUT TYPE="TEXT" NAME="name"> <P>Please enter your surname: <INPUT TYPE="TEXT" NAME="surname"> Startform while ($i < $max) { print <<Loop; <P>$question[$i] <BR> &nbsp; &nbsp; &nbsp; &nbsp;<INPUT TYPE="RADIO" NAME="answer$i" VAL +UE="$A[$i]">$A[$i]<BR> &nbsp; &nbsp; &nbsp; &nbsp;<INPUT TYPE="RADIO" NAME="answer$i" VAL +UE="$B[$i]">$B[$i]<BR> &nbsp; &nbsp; &nbsp; &nbsp;<INPUT TYPE="RADIO" NAME="answer$i" VAL +UE="$C[$i]">$C[$i]<BR> &nbsp; &nbsp; &nbsp; &nbsp;<INPUT TYPE="RADIO" NAME="answer$i" VAL +UE="$D[$i]">$D[$i]<BR> Loop $i++; } print <<Endform <P> <INPUT TYPE="SUBMIT" VALUE="Send Answers"> <INPUT TYPE="RESET" VALUE="Clear Answers"> </FORM> Endform } print <<Endbit; </BODY></HTML> 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

In reply to Multiple Choice Quiz by solex

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.