tjox has asked for the wisdom of the Perl Monks concerning the following question:
Hello so, I am very new to perl, I need to take the post data from a quiz and compare it against an array containing the correct answers. Now the answer key is to be generated from a text file, that goes like the following:
1. question?
*a. answer
b. answer
c. answer
the * means the correct answer, so I need to take very line in the file starting with a * to create the answer key.
then I need to compare the post data (results from the user's quiz against the answer key I generate.
my problem appears to be generating the answer key from the file.
so far I have tried this:
I am attempting to find the line starting with *, then add it to an array called answers. then I hope to get the following to work to do a basic correct or wrong check:my @answers = (); open(my $file, "quiz4.txt"); while (my $line = <$file>) { if ($line =~ /\A\*/) { $line = substr($line, 1); $answer = substr($line,0); push(@answers, $answer); } }
Any help, guidance, or advice is greatly appreciated! Thank you in advance!for ($i=0; $i<=9; $i++) { $thisname = $name[$i]; if (exists $formHash{$thisname}) { if ($formHash{$thisname} eq $answer[$i]) { print "Correct answer"; } else { print "incorrect answer"; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: What can I be doing wrong with this array in this particular section?
by Athanasius (Archbishop) on Jan 13, 2018 at 07:31 UTC |