
Jasmine
Administrator
/ Moderator
Jan 2, 2000, 1:01 PM
Post #3 of 7
(8414 views)
|
Alan: Instead of performing 2 mathematical functions to determine that there are 2 digits after you subtract 1117, you may wish to consider the following: $number = $data{'code'}; if (($number-1117) =~ /\b\d{2}\b/){ print "Good: $number is exactly 2 digits."; } else { print "Error: $number is not 2 digits" } Here's the line-by-line: $number = $data{'code'}; Just a cosmetic change -- I find it easier to assign form input to clean variables. if (($number-1117) =~ /\b\d{2}\b/){ Here, we're doing a regular expression that does 2 things: 1) it subtracts 1117 from $number and 2) takes the result of that subtraction and sees if the result is exactly 2 digits in length. The \d is the code for a digit in pattern matches. This will not match any letters or whitespaces -- only digits (0-9). Using \d{2} looks for 2 consecutive digits. The \b is a quantifier for a "word boundary". Without the \b, it would match 1234, because there are 2 digits within 1234. The \b in the beginning and at the end says "look for any 2 digits by themselves". If you put the \b only in the beginning, the expression would say "look for 2 digits at the beginning of the word" -- so both are needed. print "Good: $number is exactly 2 digits."; } else { print "Error: $number is not 2 digits" } Easy enough -- if $number is 2 digits, then continue onto whatever you should do if it's valid. If not, put whatever error code you'd like. Hope this helps, and best of luck to you in your Perl education! -Jasmine
|