irvson has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: do-while loop
by davido (Cardinal) on Nov 03, 2009 at 17:09 UTC | |
by JadeNB (Chaplain) on Nov 04, 2009 at 05:16 UTC | |
|
Re: do-while loop
by kennethk (Abbot) on Nov 03, 2009 at 17:14 UTC | |
|
Re: do-while loop
by CountZero (Bishop) on Nov 03, 2009 at 21:59 UTC | |
by irvson (Sexton) on Nov 03, 2009 at 22:33 UTC | |
|
Re: do-while loop
by Anonymous Monk on Nov 03, 2009 at 17:06 UTC |