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:
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);
}
}
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:
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";
}
}
}
Any help, guidance, or advice is greatly appreciated!
Thank you in advance!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|