in reply to Validation of UserInput is a positive Integer or not?
Short answer: NO, not even close!
Longer answer:
#!/usr/bin/perl use strict; use warnings; # 1149595 print "Enter number of days (1..90, no fractions or negative values): +"; my $days = <STDIN>; chomp $days; # if ($days =~ /^\s*1-90-9*\s*$/) { # the numeric "test" is hopeless +ly wrong # the "^\s* allows user to enter leading spaces # regex permits a trailing space to match despite +the chomp # ....BUT, a regex is probably the wrong way to go +... #if (0 < $days && $days <= 90 && $days =~ /^\d\d$/ ) { # makes the + exact test explicit if ( (0 < $days) && ($days <= 90) && ($days =~ /^\d\d$/) ) { # some m +ay prefer this print "Inside if, satisfied the spec: $days \n\n"; } else { print "Days entered do not meet spec: $days \n\n"; # tell the use +r specificly }
Update Added missing "use strict;" --- aaaerg! (and fixed misallignment of some comments).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Validation of UserInput is a positive Integer or not?
by Apero (Scribe) on Dec 07, 2015 at 19:27 UTC | |
by ww (Archbishop) on Dec 08, 2015 at 13:45 UTC | |
by Apero (Scribe) on Dec 08, 2015 at 14:01 UTC | |
by ww (Archbishop) on Dec 08, 2015 at 14:06 UTC |