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: Intermediate:
Using entire number strings

 



benn600
User


Nov 6, 2004, 6:46 PM

Post #1 of 4 (373 views)
Using entire number strings Can't Post

Hello everyone,

I am in the process of making a website in cgi-perl and I have ran into a problem. My site consists of something similar to a message board. In order to create pages (with 5 listed per page), I am using some formulas and the problem I have is that it says 2 is greater than 10. I remember seeing somewhere how to make it use 10 as 10 and not as 1, but I can't remember how.

Thank you very much for your assistance
----------------------------------------------------------------------------
Wink http://www.ppcpathways.com/ Wink
Visit my new site devoted to reviewing the latest pocketpc products and news that I built in cgi-perl!


KevinR
Veteran


Nov 6, 2004, 11:06 PM

Post #2 of 4 (372 views)
Re: [benn600] Using entire number strings [In reply to] Can't Post

sounds like you are comparing strings instead of digits.

Use <=> to compare numbers (digits)
Use cmp to compare strings

Use == to test equivalency of numbers
Use eq to test string equivalency
-------------------------------------------------


(This post was edited by KevinR on Nov 6, 2004, 11:08 PM)


benn600
User


Nov 7, 2004, 5:04 AM

Post #3 of 4 (367 views)
Re: [KevinR] Using entire number strings [In reply to] Can't Post

I actually figured that out after I posted it because I remember reading that you use < > for numbers. What did you post, though? It is confusing to me...all I know is !=, =, <, >, etc. When my site is closer to complete, I should post it so everyone can give me their opinion.
----------------------------------------------------------------------------
Wink http://www.ppcpathways.com/ Wink
Visit my new site devoted to reviewing the latest pocketpc products and news that I built in cgi-perl!


KevinR
Veteran


Nov 7, 2004, 8:36 AM

Post #4 of 4 (365 views)
Re: [benn600] Using entire number strings [In reply to] Can't Post

If you posted the section of code you are having problems with that will be easiest.

What I posted is the operators Perl uses to compare numbers and strings, strings can be numbers, but if numbers are compared as strings they are not compared by the overall value of the number, but are compared in an alpha betical sense, thus

1001 comes before 2 because 1 is first in an alpha betical sense even though 1001 is greater than 2 in a numeric sense.

Run this small example script if need be:


Code
#!perl -w 
use CGI qw/:standard/;
print header;

@numbers = qw(10 2 1001);

#compare and sort the numbers as strings
@nums = sort {$a cmp $b} @numbers;
print "$_<br>" for @nums;
print "<p>";

#compare and sort the numbers as numbers
@nums = sort {$a <=> $b} @numbers;
print "$_<br>" for @nums;

-------------------------------------------------

 
 


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

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