Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have to evaluate a multiple choice test and return a grade, some questions can have mutiple answer thus, you get it right if at least get one of the possible answers.
my %test = ( question1 => 'a', question2 => 'b', question3 => 'a', ); #my hash of arrays with multiple answers my %answers = ( question1 => [ 'a' ], question2 => [ 'b', 'd' ], question3 => [ 'b' ], );
The result will be: your grade is %, you answered n question correct. (until now I don't need to display which questions were correct)
for my $q1 (keys %test) { for my $a1 (0..$#$answers{$q1}){ if ($test{$q1} eq $answers{$q1}[$a1]{ $grade++; } }
So far, not results. Thanks

Replies are listed 'Best First'.
Re: Calculate grades
by DamnDirtyApe (Curate) on Apr 02, 2004 at 01:15 UTC

    I'm tempted to take this approach (assuming you're only dealing with single letter answers):

    #! /usr/bin/perl use strict; my %test = ( question1 => 'a', question2 => 'b', question3 => 'a', ); #my hash of arrays with multiple answers my %answers = ( question1 => [ 'a' ], question2 => [ 'b', 'd' ], question3 => [ 'b' ], ); my $q_count = keys %answers; my $grade = 0; for ( keys %answers ) { $grade++ if "@{$answers{$_}}" =~ /$test{$_}/i; } print "You got $grade out of $q_count right.$/";

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
      Hello DamnDirtyApe
      Could you please decode for me the regexp, so I can adapt it?
      Actually some others test, have the answer type, not the option (one letter), so I could change the regexp. Thanks

        There's not really anything to decide. Putting an array inside quotes returns a scalar with the array contents, so if $foo == [ 'a', 'b' ], then "@$foo" == "ab". The regexp searches that string for the answer, so if the answer given was "b", what were essentially doing is "ab" ~= /b/i. This will match, and the correct grade counter is incremented.

        Make sense?


        _______________
        DamnDirtyApe
        Those who know that they are profound strive for clarity. Those who
        would like to seem profound to the crowd strive for obscurity.
                    --Friedrich Nietzsche
Re: Calculate grades
by Anonymous Monk on Apr 02, 2004 at 08:59 UTC

    Perhaps something like this? Tweak it all you like :)

    #!c:/perl/bin/perl -w $|++; use strict; my @questions = ( { question => 'What perl site is a great learning tool?', answers => { perlmonks => 1, # 1 for correct answer javajunkies => 0, # 0 for incorrect answer microsoft => 0 } }, { question => 'Pick a valid color:', answers => { red => 1, # multiple correct answers blue => 1, # permitted sheep => 0, cow => 0 } } ); my $correct = 0; for my $question (@questions) { print "\n\nQ: ", $question->{'question'}, "\n", 'A: ', join(', ', keys %{$question->{'answers'}}), "\n\n"; print 'Your answer: '; chomp( my $ans = <STDIN> ); ++$correct if ($question->{'answers'}->{lc $ans}); } print "\n\nYour grade is ", +(int($correct/@questions*100)), "%.\n", "You answered $correct questions correctly.\n\n";