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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script has random hiccup

Status
Not open for further replies.

ke5jli

Technical User
Jan 1, 2007
13
0
0
US
I am creating a quiz generation script that pulls questions from a file and displays them "randomly". the script calls a .txt file which defines how many questions from each section of the pool to display. while the script functions mostly correctly, it has a random hiccup where it will display a question from the previous section or outright display the same question twice.

Code:
#!/usr/bin/perl


$| = 1;

print "Content-Type: text/html\n\n";
print "<form method=\"POST\" action=\"MYSCRIPT.cgi\">";


#sub generate_quiz {
$dn=1;
$i = 0;

open(FILE, "data.txt") ;
@lines_in_file = <FILE>;
close(FILE);			

 
open(FILE, "param.txt") ;
@params = <FILE>;
close(FILE);
foreach $linep (@params){
  ($sectionwork,$subsectionwork,$times) = split(/\t/, $linep);
  
  $sectionwork =~ s/\s+//g;
  $subsectionwork =~ s/\s+//g;
  $times =~ s/\s+//g;
  
  
  print "<br><br><b>Debug Line $sectionwork$subsectionwork - $times question(s)</b><br><br>\n\n";
   
   
   
   
  foreach $line (@lines_in_file){
    ($section,$subsection,$number,$answer,$part,$question,$questionimage,$choicea,$choiceb,$choicec,$choiced,$supportingimage,$supportingtext) = split(/\t/, $line);
    
    
    if ($section eq $sectionwork && $subsection eq $subsectionwork){   
      $questions_to_ask[$i] = $line;
      $i++;
      
    }			
  }
  
  local(@temp);
  push(@temp, splice(@questions_to_ask, rand(@questions_to_ask), 1)) 
  while @questions_to_ask;
   
  @questions_to_ask = @temp;
   
  for($i = 0; $i <= $times - 1; $i++){
    $q_num = $i + 1;
    ($section,$subsection,$number,$answer,$part,$question,$questionimage,$choicea,$choiceb,$choicec,$choiced,$supportingimage,$supportingtext) = split(/\t/, $questions_to_ask[$i]);
    $asked[$i] = $questions_to_ask[$i];
    $dispnum++;
    
    print <<end
    <table border="0" cellpadding="2" style="border-collapse: collapse" width="100%">
    <tr>
    <td width="2%" nowrap>$dispnum)</td>
    <td colspan="2">$section$subsection$number $part</td>
    </tr>
    <tr>
    <td width="2%" nowrap>&nbsp;</td>
    <td colspan="2">$question</td>
    </tr>
    <tr>
    <td width="2%" nowrap>&nbsp;</td>
    <td width="2%"><input type="radio" value="A" name="Q$dispnum"></td>
    <td width="94%">$choicea</td>
    </tr>
    <tr>
    <td width="2%" nowrap>&nbsp;</td>
    <td width="2%"><input type="radio" value="B" name="Q$dispnum"></td>
    <td width="94%">$choiceb</td>
    </tr>
    <tr>
    <td width="2%" nowrap>&nbsp;</td>
    <td width="2%"><input type="radio" value="C" name="Q$dispnum"></td>
    <td width="94%">$choicec</td>
    </tr>
    <tr>
    <td width="2%" nowrap>&nbsp;</td>
    <td width="2%"><input type="radio" value="D" name="Q$dispnum"></td>
    <td width="94%">$choiced</td>
    </tr>
    </table>
end
;

 
  }
  
}
print "</form>";
#} #END SUB GENERATE QUIZ

for ease of explanation, the "working script can be viewed at
The Debug Line shows the section and subsection that the current question should be coming from however, this is not always the case. Also, this seems to happen every time the script is run but not in the same location twice.

a zip with all associated files can be found at

Thank You for your time once again!! [bigsmile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top