CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Simple Number Check

 



AlanBell
Deleted

Jan 2, 2000, 6:09 AM

Post #1 of 7 (3630 views)
Simple Number Check Can't Post

I'm new to CGI's and Perl. I've just spent the last few days trying to read through the Perl manual to no avail. I just wanted to write a simple script for a form to check if a number is valid. Easy to do in Excel etc. but my syntax is all wrong.

A. I supply unique six digit number to customer to obtain a free sample. This number needs to be entered on the form when the free sample is requested. (I need some limit to prevent multiple requests)
B. The number is formed by squaring a seed number and adding 1,117.
C. To check the number, I firstly subtract 1,117 and then take the square root. It should be a whole integer if the number is valid.

My code looked something like this. (the field on the form is named "code")

$a == ($data{'code'} - 1117);
$b == sqrt($a);
$c == int($b);
if ($b != $c)
{
print "Content-type: text/html\n\n";
print <<"EXIT";
<body bgcolor="ffffff">
$data{'code'}- This code is invalid!
EXIT
exit;
}

I don't understand if the syntax for "sqrt" and/or "int" are correct but I suspect that the problem is in the "$a", "$b" and "$c" as I have introduced them without definition.
There must be a simple way to perform this calculation but it is beyond my expertise.

I would greatly appreciate any help in this matter.
Thanks,
Alan


Borderline
Deleted

Jan 2, 2000, 7:45 AM

Post #2 of 7 (3630 views)
Re: Simple Number Check [In reply to] Can't Post

I do not think this is the best way to handle this sort of thing but here is the problem with the code.
First == is a comparison operator. You are using it to try and set a value
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$a == ($data{'code'} - 1117);
$b == sqrt($a);
$c == int($b);</pre><HR></BLOCKQUOTE>
Should be written like this
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$a = ($data{'code'} - 1117);
$b = sqrt($a);
$c = int($b);</pre><HR></BLOCKQUOTE>
Also what if $a turns out to be negative? This will crash the script. You should probably test this before trying to take the square of it.
Try something like this:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$ent = $data{code} - 1117;
if ($ent < 0) {form_error()}

$ent = sqrt($ent);
$num = int($ent);

if ($num != $ent) {form_error()}

print <<END_OF_HTML;
Content-type: text/html

<HTML>We Made IT!!</HTML>
END_OF_HTML

sub form_error {

print <<"EXIT";
Content-type: text/html

<HTML>
<body bgcolor="ffffff">
$data{'code'}- This code is invalid!
</HTML>
EXIT
exit;
}</pre><HR></BLOCKQUOTE>

Scott


Jasmine
Administrator / Moderator

Jan 2, 2000, 1:01 PM

Post #3 of 7 (3630 views)
Re: Simple Number Check [In reply to] Can't Post

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! Smile

-Jasmine


AlanBell
Deleted

Jan 2, 2000, 5:44 PM

Post #4 of 7 (3630 views)
Re: Simple Number Check [In reply to] Can't Post

Simple when you know how!
My clumsy code even looks a bit neater now.
Thanks to both Scott and Jasmine.


Borderline
Deleted

Jan 2, 2000, 7:29 PM

Post #5 of 7 (3630 views)
Re: Simple Number Check [In reply to] Can't Post

Alan:

Glad we could be of help!

Jasmine:

I don't understand your answer. Why would you want to test if it were 2 digits?

Scott


Jasmine
Administrator / Moderator

Jan 2, 2000, 7:50 PM

Post #6 of 7 (3630 views)
Re: Simple Number Check [In reply to] Can't Post

Alas, I get the dunce cap of the day... for some (dumb) reason, I'd interpreted the original message as 4 out of 6 digits will be removed, making it necessary to check for the existince of a double-digit number.

My apologies to all for the confusion...


AlanBell
Deleted

Jan 2, 2000, 9:00 PM

Post #7 of 7 (3630 views)
Re: Simple Number Check [In reply to] Can't Post

Jasmine,
I understood that you had not understood the original senario.
However, I liked your explanation.
I'm just waiting to find a need for the script.
I think it's a case of the chicken and the egg.
Thanks again,
Alan

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives