Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

What can I be doing wrong with this array in this particular section?

by tjox (Initiate)
on Jan 13, 2018 at 06:04 UTC ( #1207183=perlquestion: print w/replies, xml ) Need Help??
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:

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!

Replies are listed 'Best First'.
Re: What can I be doing wrong with this array in this particular section?
by Athanasius (Chancellor) on Jan 13, 2018 at 07:31 UTC

    Hello tjox, and welcome to the Monastery!

    You say “my problem appears to be generating the answer key from the file”, but you don’t say why that is. As far as I can see, the code you’ve shown is doing the job. Here is what I get (using Data::Dump to show the contents of @answers):

    17:17 >perl ["a. answer \n", "b. answer \n", "c. answer \n"] 17:18 >

    That said, there are a number of ways in which the code could be improved:

    • Always begin each script with use strict; and use warnings;
    • The line $answer = substr($line,0); does nothing
    • When opening a file, prefer the 3-argument form: open(my $file, '<', 'quiz4.txt');
    • Always check the result of an open or close operation: either use autodie;, or test explicitly:
      my $file = 'quiz4.txt'; ... open(my $fh, '<', $file) or die "Cannot open file '$file' for reading, stopped"; ... close $fh or die "Cannot close file '$file', stopped";
    • Trim newline characters from lines using chomp or via a regular expression (see below).

    In addition, I would read and store question numbers as well as the answers; and I would separate the letter code of the correct answer from the description that follows, thereby making it easier to test whether a given answer is correct:

    use strict; use warnings; use Data::Dump; my %answers; my $question; while (my $line = <DATA>) { if ($line =~ / ^ (\d+) \. /x) { $question = $1; } elsif ($line =~ / ^ \* (\w+) \. \s* (.*?) \s* $ /x) { warn "Overwriting answer for question $question" if exists $answers{$question}; $answers{$question} = [$1, $2]; } } dd \%answers; __DATA__ 1. question? *a. answer begin end b. answer c. answer 2. question? a. answer *b. answer c. answer 3. question? a. answer b. answer *c. answer


    17:26 >perl { 1 => ["a", "answer begin end"], 2 => ["b", "answer"], 3 => ["c", "answer"], } 17:27 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1207183]
Approved by Athanasius
and the voices are still...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2018-06-19 23:11 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.