I'm new to Perl. (I prefer "neophyte" to "newbie.") I'll post the code, then tell you what I'm trying to do.

do { print "\nPick a letter between a and d.\n "; chomp($reply = <STDIN>); } while (lc $reply ne ('a' .. 'd'));

I want the user to answer multiple choice questions by choosing the letters "a" thru "d." If any other letter is entered, I want the script to show the first "print" line. Once a letter "a" thru "d" is entered, I want the script to continue to the question. I tried "while" and "if" loops (without "do"), and I ended up with a loop that just kept repeating "Pick a letter between a and d." Using "do" -- as shown -- has, at least, created the situation where the "Pick a letter..." sentence appears once, and then the script waits for another input. BUT...even when I enter a letter "a" thru "," the script keeps repeating "Pick a letter..." I've gotten the impression that the range operator works only with arrays, which is not the case here.

So, I have two questions: 1) How do I specify that the input must be in the range "a thru d?" 2) How do I get the script to move on to the question once an accepted letter has been entered.

Thanks.


In reply to do-while loop 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.