Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Answer: how do i work this

( #11230=categorized answer: print w/ replies, xml ) Need Help??

Q&A > input and output > How can I validate user input? contributed by Russ

Here's one way to do this:
my $start = $ARGV[0]; while (1){ print "Please Enter the Start-Level (0-7,?)[0]: "; chomp($start = <>); # Check for non-numeric characters if ($start =~ /\D/){ print "\nPlease enter a number (0-7) or ? for help\n"; next; } last if 0 <= $start and $start <= 7; # Check for a help request if ($start eq '?') { print " **** internally coded help information **** \n"; next; } print "\nPlease enter a number between Zero and Seven, inclusive\n"; } print $start;
First, we check that the input is numeric (as you started to do in your commented-out code) to prevent errors if the user enters non-numeric characters.
If the input is valid, we just exit the loop.
If not, check for a help request and, if not '?', print a generic error message and redo the loop.

There are a million different ways to do this, and this is only one of them...

Russ

Comment on Answer: how do i work this
Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2015-07-31 03:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls