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:
Length checks not working correctly

 



skirrow
stranger

Jan 14, 2003, 4:15 AM

Post #1 of 13 (1563 views)
Length checks not working correctly Can't Post

Hi,

I'm using the following code to check the length of usernames and passwords inputted is greater than 7 characters.

$username = $input_username if (length($input_username) lt "7");
$password = $input_password if (length($input_password) lt "7");

For some reason, any data inputted that is greater than 9 characters in length is rejected. How do I modify it to accept greater length inputs?

Thanks,

Neil


Paul
Enthusiast

Jan 14, 2003, 8:15 AM

Post #2 of 13 (1547 views)
Re: [skirrow] Length checks not working correctly [In reply to] Can't Post

Of course it will be rejected, your code is only accepting anything less than 7 chars.


skirrow
stranger

Jan 14, 2003, 1:26 PM

Post #3 of 13 (1542 views)
Re: [Paul] Length checks not working correctly [In reply to] Can't Post

No, sorry, that was a typo. I was typing the code to post not copying and pasteing. Even with gt it doesn't accept over 9 characters.

- Neil
--
http://www.locked-area.com/
http://www.opencrypt.com/


jryan
User

Jan 14, 2003, 2:13 PM

Post #4 of 13 (1538 views)
Re: [skirrow] Length checks not working correctly [In reply to] Can't Post


Code
$username = $input_username if (length($input_username) < 100);


You were using the string compare operators rather than the numeric compare operators. String operators compare by ascii value, which isn't what you want.


skirrow
stranger

Jan 14, 2003, 4:24 PM

Post #5 of 13 (1536 views)
Re: [jryan] Length checks not working correctly [In reply to] Can't Post

Hi,

That won't stop inputs over 7 characters in length.

- Neil
--
http://www.locked-area.com/
http://www.opencrypt.com/


jryan
User

Jan 14, 2003, 7:00 PM

Post #6 of 13 (1532 views)
Re: [skirrow] Length checks not working correctly [In reply to] Can't Post

Then what in the hell do you want? I thought you said that you wanted to accept lengths of different inputs? It really isn't hard to change the number.


skirrow
stranger

Jan 15, 2003, 5:17 AM

Post #7 of 13 (1526 views)
Re: [jryan] Length checks not working correctly [In reply to] Can't Post

As I said, even when using greater than 7, only inputs of 8 and 9 characters in length are accepted. Any greater than 9 are rejected.

How do I stop this, that is my question?

I want all inputs greater than 7 characters in length to be accepted, yet once it hits double digits it doesn't seem to work.

- Neil
--
http://www.locked-area.com/
http://www.opencrypt.com/


Paul
Enthusiast

Jan 15, 2003, 6:20 AM

Post #8 of 13 (1525 views)
Re: [skirrow] Length checks not working correctly [In reply to] Can't Post

You are making this very confusing for everyone. A couple of posts up you said:

>>
That won't stop inputs over 7 characters in length.
<<

Now you are saying...

>>
I want all inputs greater than 7 characters in length to be accepted
<<

So in the words of jryan "What the hell do you want?" Cool


skirrow
stranger

Jan 15, 2003, 6:56 AM

Post #9 of 13 (1524 views)
Re: [Paul] Length checks not working correctly [In reply to] Can't Post

My apologies, I should proof read!!

Let me try and explain again. I want all inputs that are greater than 7 characters in length to be excepted. However, when I use the following code:

Code
$password = $input_password if (length($input_password) gt "7");

Only inputs of 8 and 9 characters in length are accepted. Not 10 characters and above. This is my problem. Is this a bug with Perl or what as surely it should only reject inputs less than 8 characters in length.

Sorry about the confusion. :)
- Neil
--
http://www.locked-area.com/
http://www.opencrypt.com/


thodi
stranger

Jan 15, 2003, 7:41 AM

Post #10 of 13 (1521 views)
Re: [skirrow] Length checks not working correctly [In reply to] Can't Post


In Reply To
Let me try and explain again. I want all inputs that are greater than 7 characters in length to be excepted. However, when I use the following code:

Code
$password = $input_password if (length($input_password) gt "7");


Somebody already pointed out that you should use a numerical comparison instead of a string comparison. Run this script and think about the results:

Code
my @pws = qw/1234567 12345678 654321012345 this_should_be_enough 2short/; 

for (@pws) {

print "'$_' numeric: ";
if (length > 7) {
print "accepted!\n"
} else {
print "rejected!\n"
}

print "'$_' string: ";
if (length gt "7") {
print "accepted!\n"
} else {
print "rejected!\n"
}
print "\n";
}



Paul
Enthusiast

Jan 15, 2003, 9:33 AM

Post #11 of 13 (1519 views)
Re: [skirrow] Length checks not working correctly [In reply to] Can't Post

This will work as expected:


Code
$password = $input_password if (length $input_password > 7);


You can use it on the condition that you try thodi's suggestion first :)


davorg
Thaumaturge / Moderator

Jan 16, 2003, 1:09 AM

Post #12 of 13 (1505 views)
Re: [skirrow] Length checks not working correctly [In reply to] Can't Post


In Reply To
My apologies, I should proof read!!

Let me try and explain again. I want all inputs that are greater than 7 characters in length to be excepted. However, when I use the following code:

Code
$password = $input_password if (length($input_password) gt "7");

Only inputs of 8 and 9 characters in length are accepted. Not 10 characters and above. This is my problem. Is this a bug with Perl or what as surely it should only reject inputs less than 8 characters in length.


Not a bug with Perl, but a bug with your understanding of Perl.

The comparison operators that look like words ('eq', 'ne', 'gt', 'cmp', etc) do a comparison based on the ASCII value of their operands. Under this system the string "10" sorts before the string "2" because "1" has a lower ASCII value than "2". This is identical to the processing that makes "an" sort before "I".

If you want to compare the values as numbers, then you'll need to use the numerical comparison operators. They're the ones that look like mathematical symbols - '=='. '<>', '>', '<=>, etc.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


skirrow
stranger

Jan 16, 2003, 2:54 AM

Post #13 of 13 (1503 views)
Re: [davorg] Length checks not working correctly [In reply to] Can't Post

Thank you for clarifying and sorting that problem.

- Neil
--
http://www.locked-area.com/
http://www.opencrypt.com/

 
 


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

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