#!/usr/bin/perl #invocation: # ./quiz.pl protein.txt @answers = (); @prompts = (); $cond = 0; $hold = ""; while(<>){ if(/^/){ ($output) = ($_ =~ /^(.*)<\/name>/); push(@answers, $output); } elsif(/^/){ $_ =~ s///; $_ =~ s/\n//; $_ =~ s/\r//; if($_ =~ /<\/desc>/){ $_ =~ s/<\/desc>//; push(@prompts, $_); } else{ $hold = $_; $cond = 1; } } elsif( /<\/desc>$/ ){ $cond = 0; $_ =~ s/\n//; $_ =~ s/\r//; $_ =~ s/<\/desc>//; $output = "$hold $_"; push(@prompts, $output); } elsif( cond == 1){ $_ =~ s/\n//; $_ =~ s/\r//; if($_ =~ /<\/desc>/){ $cond = 0; $_ =~ s/<\/desc>//; $output = "$hold $_"; push(@prompts, $output); } else{ $hold = $_; } } } $cnt = 0; @answer_key=(); foreach $item (@answers){ push(@answer_key, $cnt); $cnt++; } $total = $cnt; $cnt = 0; #scramble quiz foreach $item (@answers){ $r_num = rand($total); $a_hold = $prompts[$cnt]; $prompts[$cnt] = $prompts[$r_num]; $prompts[$r_num] = $a_hold; $ak_hold = $answer_key[$cnt]; $answer_key[$cnt] = $answer_key[$r_num]; $answer_key[$r_num] = $ak_hold; $cnt++; } $cnt = 0; $score = 0; $number=@prompts; foreach $thing (@answers){ $linecnt=0; foreach $choice (@answers){ $item = $linecnt+1; print "$item) $choice\t"; if($item%3 == 0) { print "\n"; } $linecnt++; } print "-----------------\n"; print "$prompts[$cnt]\n"; print "Select correct protein: "; $response = ; chomp($response); $response--; if( $response < $number && $response == $answer_key[$cnt]){ print "CORRECT\n"; $score++; } else{ print "WRONG\n"; } $cnt++; } print "SCORE: $score out of $number\n";