in reply to Control Structure problem, mistake can't be found

If possible it is a really good idea to keep related data together so that it can be handled in a common fashion. Perl lets you do that by using structures like arrays of array and hashes etc. In your case it makes sense that you keep the question and answer pairs together. You could use an array for each pair, but it's easier to understand the code if you use a hash instead. Consider:

use strict; use warnings; my @quiz = ( {ask => "Name the definitive rock band..", answer => "The Rolling Stones" }, {ask => "What is their best song?", answer => "Moonlight Mile" }, {ask => "What is their best album?", answer => "Sticky Fingers" } ); my @incorrect; for my $question (@quiz) { print "$question->{ask}: "; my $guess = <STDIN>; chomp $guess; if (lc $guess eq lc $question->{answer}) { print "You Rock!\n"; } else { print "You Suck!\n"; # if incorrect, reply and save answer to + be calculated push @incorrect, [$question, $guess]; } } my $grade = (@quiz - @incorrect) / @quiz * 100; print "Grade: $grade\n";

Note that there is now no need for a counter to access the question and answer. Because each question and answer is paired it is trivial to add questions or change their order and know that the right answer is matched with each question.

Note too that now the entire question and answer are stored along with the answer given in the incorrect list. Also the grade uses the count of elements in the two arrays for the calculation so that a manually calculate constant isn't required - the grade calculation remains correct if the number of questions changes.


Perl reduces RSI - it saves typing

Replies are listed 'Best First'.
Re^2: Control Structure problem, mistake can't be found
by koolgirl (Hermit) on Aug 25, 2008 at 22:22 UTC
    Just wondering, this syntax:
    my @quiz = ( {ask => "Name the definitive rock band..", answer => "The Rolling Stones" }, {ask => "What is their best song?", answer => "Moonlight Mile" }, {ask => "What is their best album?", answer => "Sticky Fingers" } );
    it's object oriented Perl, right? Or is it just another way to give hash keys a value? I am only familiar with this type of syntax for the same thing:
    $myquiz{"question_here"} = "answer here";
    I've just never seen it done in this way, and I was wondering what the difference was (which when I looked it up, I thought I understood it to be "object oriented"), and if there is a benefit of chosing one over the other?

      Let's take it a little bit at a time:

      my @quiz = (...);

      initializes an array from a list.

      {...}

      generates a reference to a hash. A reference is a "handle" that you can use to refer to something else. So:

      my @quiz = ({...}, {...}, {...});

      generates an array containing references to three hashes. Each hash is of the form:

      {ask => "...", answer => "..."}

      The => is the "fat comma". It's exactly like an ordinary comma, except that it pretends the (non-white space) sequence of characters to its left has quotes around it. ask => "..." is the same as 'ask', "...". The fat comma is a handy way of generating a list to initialize a hash and is often used as a heads up to indicate that two items in a list are a pair.

      The intent is to generate a data structure comprised of an array containing a list of hashes where each hash represents a question and answer pair. Other information could be added to each hash such as a weighting for the question (so harder questions are worth more) for example:

      my @quiz = ( {ask => "Name the definitive rock band..", answer => "The Rolling Stones", value => 10, }, {ask => "What is their best song?", answer => "Moonlight Mile" value => 20, }, {ask => "What is their best album?", answer => "Sticky Fingers" value => 5, }, );

      Perl reduces RSI - it saves typing
      No, it's not OO perl. It's just declaring an array of hashrefs, or as commonly known, an array of hashes aka an AoH. See perldsc for more on this.

      For object oriented programming in Perl, you going to need modules (see perlmod) and bless. If you're interested in OOP, you should probably look at perlboot and perltoot before anything else.