Hashes are good for this sort of work, the question number is the hash key and the answer is the hash value related to that key, read this intro as well, there are many ways to declare a hash, here I assigned an array to a hash...the program uses a label called "CHECK" to control its flow...If the letter choice corresponds to a key in the hash then that hash value would be defined and you would see the answer, else the program would go back to the label "CHECK:" and prompt you to answer the question once again..
use strict; use warnings; my @array = qw(a Grant b Yoda c Liberace d None); my %hash= @array; CHECK: print "Who's Buried in Grant's Tomb\n"; print <<ENDING; a. Grant b. Yoda c. Liberace d. None of the above. ENDING while(chomp(my $answer = <STDIN>)){ if(defined $hash{$answer}){ print "your Answer is \"$hash{$answer}\".\n"; print "Thank You.\n"; #do anything with the Answer.... exit; }else{ print "Please Pick a letter between A - D\n"; goto CHECK; } }


Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

In reply to Re: while loop almost correct by biohisham
in thread while loop almost correct 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.