Gossamer Forum
Home : General : Perl Programming :

Conversion between strings and numbers prob

Quote Reply
Conversion between strings and numbers prob
Hi all,

I've got a problem in a Perl CGI script that I'm writing (running WinXP), where it tries to check the validity of user input to see if certain field values have the allowed range of 1 to 10. However the script doesn't work properly. I added some debugging stuff to it, to show what users entered and what the program processed:

The output from not_valid() is '7'
In HEX: 0x0
Is '7' bigger than '5'? NO
Is '7' less than '20'? YES

The QUERY_STRING that was sent by the browser was:
test_cgi.pl?name=SIMON&email=SIMON&q1=7&q2=6&q2a=None.&q3=8&q4=7&q5=9&q6=None.&q7=7&comments=None.

The place where I think the problem is, is the function not_valid(), where it actually checks the thing. Here's the snippet of code for it:
sub not_valid() {
$n = shift;

# Debug: Here's basically the problem
print "<p>The output from <code>not_valid()</code> is ", $n, "</p>\n";
print "<p>In HEX: <code>0x", hex($n), "</code></p>\n\n";
print "<p>Is $n bigger than \'5\'? ";

if (($n gt "5") || ($n gt '5') || ($n > '5')) {
print "YES</p>\n\n";

if ($n gt "5") {
print "<p>YES by string comparison (WHAT???)</p>\n\n";

}

elsif ($n gt '5') {
print "<p>YES by string \'\' comparison (WHAT???)</p>",
"\n\n";

}

else {
print "<p>YES by numerical comparsion \'>\' using " .
"ASCII equivalent of 5 (SHIT!)</p>\n\n";

}

}

else {
print "NO</p>\n\n";

}

print "<p>Is $n less than \'20\'? ";

if (($n lt "20") || ($n lt '20') || ($n < '20')) {
print "YES</p>\n\n";

}

else {
print "NO</p>\n\n";

}

# End of Debug

if (($n > 0) && ($n < 11)) {
return 0;

}

else {
return 1;

}

}

Any help would be great. I've been stuck on this prob for some time, and I've tried asking in #perl chat rooms on IRC, but to no avail.

Thanks alot,

Simon Liu
Quote Reply
Re: [Simonliu] Conversion between strings and numbers prob In reply to
Why do you use the following?

if (($n gt "5") || ($n gt '5') || ($n > '5')) {


I would use;

if ($n > 5) {

.. i.e don't use the 'gt' part...

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Conversion between strings and numbers prob In reply to
Hey Andy,

But that is the problem: $n > [number] doesn't work under any circumstances. Say if I input to the CGI Perl program for q1=5, then $n < 10 returns false! Plus, when I print out the hexadecimal equivalent of what $n contains, I get 0x0 => 0. I don't get why Perl doesn't switch from strings to numbers in this case, and what functions you could use to manually convert strings to numbers.

Thanks,

Simon
Quote Reply
Re: [Simonliu] Conversion between strings and numbers prob In reply to
How are you grabbing the input via CGI.pm? It shouldn't translate into hex at all. I've never had such a problem Unsure

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Conversion between strings and numbers prob In reply to
I've tried two ways actually. One was manually going through the entire QUERY_STRING, substituting the %[hex] with they proper ASCII equivalents, and then splitting the string into key and value pairs. I've also done it using the CGI library, but they all give the same results.

In any case, I've attached the script that's coughing up all these problems.