Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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!


In reply to What can I be doing wrong with this array in this particular section? by tjox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 23:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found