First off, always use strictures (use strict; use warnings;).

The major problem is that the questions are embedded in the code. That makes the code incredibly fragile and horrific to maintain. Move your questions out into a data structure, or better still a file (then build the data structure). Consider:

use strict; use warnings; #variable initialization my @questions = ( {question => "Question #1: Who is buried in Grant's tomb?", answers => [ "Grant", "Lincoln", "Frank", "None of the above.", ], answer => 'a', }, ); my $wrong_count = 0; for my $question (@questions) { printQuestion ($question); print "\nThe answer is: "; $question->{reply} = lc <STDIN>; chomp $question->{reply}; if ($question->{reply} eq $question->{answer}) { print "Correct\n\n"; next; } ++$wrong_count; } print "\n\n"; exit if ! $wrong_count; print "Correct answers:\n\n"; for my $question (@questions) { my $reply = ord ($question->{reply}) - ord ('a'); my $ans = ord ($question->{answer}) - ord ('a'); next if $reply == $ans; printQuestion ($question); print "\nThe answer is: $question->{answers}[$ans]\n"; if ($reply > @{$question->{answers}}) { print "You didn't give a valid answer\n" } else { print "Your answer was: $question->{answers}[$reply]\n"; } print "\n"; } sub printQuestion { my ($question) = @_; my $letter = 'A'; print "$question->{question}\n"; for my $answer (@{$question->{answers}}) { print "$letter: $answer\n"; ++$letter; } }

True laziness is hard work

In reply to Re: Best code approach for this goal by GrandFather
in thread Best code approach for this goal by irvson

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.