Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: restart program on wrong user input.

by DigitalKitty (Parson)
on Aug 01, 2004 at 23:36 UTC ( #379149=note: print w/ replies, xml ) Need Help??


in reply to restart program on wrong user input.

Hi BioGeek.

In the spirit of diversity, I'd like to offer this contribution:

#!/usr/bin/perl -w use strict; my $num; while() { print 'Enter a number between 1 and 10 (inclusive): '; chomp( $num = <STDIN> ); if( $num gt "9" ) { print "Numbers only. That was a string!\n"; next; } if( $num < 1 || $num > 10 ) { print "Please enter a value greater than one or less than 10\n"; next; } print "Thank you. You entered $num\n"; last; }


Hope this helps,
-Katie


Comment on Re: restart program on wrong user input.
Download Code
Re^2: restart program on wrong user input.
by Aristotle (Chancellor) on Aug 02, 2004 at 00:39 UTC
    $num gt "9"

    That made me blink for a second before I realized what was going on. Then I noticed you're missing the other half of the condition ($num lt "0").

    You'd do better to check whether non-digits match in $num:

    $num =~ /\D/

    This is self-documenting and complete.

    Makeshifts last the longest.

      Well, now I see why the credo of perl is TIMTOWTDI!

      Thaks to everyone who provided input to this simple newbie question.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://379149]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2014-07-29 22:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls