Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting random answers for a quiz 1

Status
Not open for further replies.

audrey07

Programmer
Feb 8, 2007
3
US
I am new to cgi, but with the help of several tutorials and forum searches I have been able to customize this script until this point. The quiz script already displays the questions randomly, but the answer choices are displayed in the order that I enter them. What do I need to do to tell it to make the answer choices display randomly? I have tried everything I can think of, but nothing has worked. Any help is GREATLY appreciated! Here's the entire sub:

sub ShowQuiz {
my $quiz = shift;
my $num_ques = getNumLine("$quiz.db");

$limit = param('number') > 0 ? param('number') : $num_ques;
$limit = $num_ques if $limit > $num_ques;

for ($i=0;$i<$num_ques;$i++) {$array[$i] = $i;}
for ($i=0;$i<$num_ques;$i++) {
$x = $array[int(rand($num_ques))];
$y = $array[int(rand($num_ques))];

$temp = $array[$x];
$array[$x] = $array[$y];
$array[$y] = $temp;
}

splice(@array, $limit);

flock(2, DB);
open(DB, "$db_dir/$quiz.db") or error("Cannot open $quiz.db: $!");
@lines = <DB>;
close(DB);
flock(8, DB);

print "<form action=\"$script\" method=\"POST\">\n";
print "<table border=0 cellspacing=0 cellpadding=0 bgcolor=#FFFFFF width=95%><tr><td>\n";
print "<table border=0 cellspacing=1 cellpadding=4 width=100%>\n";
print "<tr><td bgcolor=#FFFFFF><font$font_para size=4><b><i>".getQuizName($quiz)."</i></b></font><br></td></tr>\n";

$i = 1;
foreach $linenum (@array) {
my $j = 'color' . (($i % 2) + 1);
($qtype, $question, $qimage, $answer, $aimage, $explanation, $choice) = split(/\|\|/, $lines[$linenum]);

print "<tr bgcolor=#DFE9F9><td width=100%><font$font_para size=2><b>$i. $question</b>";
print "<br><img src=\" if $qimage ne "";
if ($qtype eq "mc") {
chomp($choice);
@choices = split(/\`\`/, $choice);
foreach $choice (@choices) {
print "<br><input type=\"radio\" name=\"$linenum\" value=\"$choice\">$choice";
}
}
if ($qtype eq "sa") {
print "<br>Answer: <input type=\"text\" name=\"$linenum\" size=30>";
}
if ($qtype eq "nu") {
print "<br>Answer: <input type=\"text\" name=\"$linenum\" size=30> (Enter only numeric value)";
}
if ($qtype eq "tf") {
print "<br><input type=\"radio\" name=\"$linenum\" value=\"t\">True <input type=\"radio\" name=\"$linenum\" value=\"f\">False";
}

print "</font></td></tr>\n";

$i++;

}
 
yikes! less code please. More explanation of what parts of the code are doing what. Use the code tags too please.

[ignore]
Code:
[/ignore][/b]
your code here
[b][ignore]
[/ignore]


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Lol, sorry about that. I'll try again.

I think the part I need help with is here (or at least this is the part that I have been editing, doing trial and error on:

Code:
 if ($qtype eq "mc") {
            chomp($choice);
            @choices = split(/\`\`/, $choice);
            foreach $choice (@choices) {
                print "<br><input type=\"radio\" name=\"$linenum\" value=\"$choice\">$choice";
                }

I believe these lines tell it if the question is multiple choice (mc) then post the available answer choices for that question. How can I tell it to post them randomly (i.e. the answers in a different order each time the page is reloaded).

Thank you, thank you for at least responding!!
 
Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]List::Util[/green] [red]qw/[/red][purple]shuffle[/purple][red]/[/red][red];[/red]
[olive][b]if[/b][/olive] [red]([/red][blue]$qtype[/blue] eq [red]"[/red][purple]mc[/purple][red]"[/red][red])[/red] [red]{[/red]
   [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red]([/red][blue]$choice[/blue][red])[/red][red];[/red]
   [url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@choices[/blue] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red]([/red][red]/[/red][purple][purple][b]\`[/b][/purple][purple][b]\`[/b][/purple][/purple][red]/[/red], [blue]$choice[/blue][red])[/red][red];[/red]
   [blue]@choices[/blue] = [maroon]shuffle[/maroon][red]([/red][blue]@choices[/blue][red])[/red][red];[/red]
   [olive][b]foreach[/b][/olive] [blue]$choice[/blue] [red]([/red][blue]@choices[/blue][red])[/red] [red]{[/red]
      [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]<br><input type=[purple][b]\"[/b][/purple]radio[purple][b]\"[/b][/purple] name=[purple][b]\"[/b][/purple][blue]$linenum[/blue][purple][b]\"[/b][/purple] value=[purple][b]\"[/b][/purple][blue]$choice[/blue][purple][b]\"[/b][/purple]>[blue]$choice[/blue][/purple][red]"[/red][red];[/red]
   [red]}[/red]
[red]}[/red]

Core (perl 5.8.8) Modules used :
[ul]
[li]List::Util - A selection of general-utility list subroutines[/li]
[/ul]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
if that works, update any other forums you have this question posted on to let them know you have it working. Feel free to post the code to show how you got it working in case anyone has comments or suggestions.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Ahhh... thank you! You are a god :) I had seen the utilities being used in other examples, but for some reason I thought you needed to insert the whole utility into the script as well... shows my knowledge, huh. Thank you again. And yes, I had already planned on posting the results in the other forum. Off to do that right now. Thanks again :) ~ Audrey
 
you're welcome [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top