ColdFusion Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreColdFusion Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 28th, 2011, 04:50 PM
jimmydean101 jimmydean101 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2011
Posts: 3 jimmydean101 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 56 sec
Reputation Power: 0
String comparison issues: 0180 and 00180 are seen as EQ

Hello,

I am looping over some session variables and doing comparisons to look for duplicate values. ColdFusion is seeing 0180 EQ 00180 although these values are indeed different. I've tried the ToString() function hoping that would make a difference but no.

Example:

ToString(session.var[a]) EQ ToString(session.var[b])

Where a = 0180 and b = 00180

Coldfusion returns TRUE

Any ideas on how to make these different in the eyes of ColdFusion?

Thanks!

Reply With Quote
  #2  
Old July 28th, 2011, 07:05 PM
webmandman webmandman is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2009
Posts: 7 webmandman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 39 m 27 sec
Reputation Power: 0
Compare()

Jimmy,

Try using compare(string1,string2):

<cfscript>
num1 = 0081;
num2 = 00081;
comparison = Compare(ToString(num2),ToString(num1));
WriteOutput(comparison);
</cfscript>

The result here is -1, meaning num2 is smaller than num1. But all you want to know is if they are equal. If they are equal, result is 0.

Reply With Quote
  #3  
Old July 28th, 2011, 07:18 PM
webmandman webmandman is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2009
Posts: 7 webmandman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 39 m 27 sec
Reputation Power: 0
ToString()

You don't even have to use ToString()

<cfscript>
num1 = 0081;
num2 = 0081;
comparison = Compare(num2,num1);
WriteOutput(comparison);
</cfscript>

Reply With Quote
  #4  
Old July 28th, 2011, 07:50 PM
jimmydean101 jimmydean101 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2011
Posts: 3 jimmydean101 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 56 sec
Reputation Power: 0
@webmandman - thanks for the response

I'm trying to figure out here what the compare() is really doing here. Adobe docs say 'The compare function performs a case-sensitive comparison of two strings.' But, the results make is sound like it's doing something similar to the Len() function on both strings, then letting you know if either is more bytes in length than the other or the same.

I'm gonna fiddle with this tomorrow and also try combining this with EQ, so for example:

<cfset test = Compare(string1, string2)>
<!--- if string1 and string2 match, and their length is equal resulting in a 0 --->
<cfif (string1 EQ string2) AND ( test EQ 0)>
<!--- Then we have a match --->
<cfelse>
<!--- we don't have a match --->
</cfif>

Reply With Quote
  #5  
Old July 29th, 2011, 11:17 AM
jimmydean101 jimmydean101 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2011
Posts: 3 jimmydean101 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 56 sec
Reputation Power: 0
Resolved

This works:


<cfif (string1 EQ string2) AND ( compare(string1,string2) EQ 0)>
<!--- Then we have a match --->
<cfelse>
<!--- we don't have a match --->
</cfif>

Reply With Quote
  #6  
Old August 1st, 2011, 01:02 PM
cfSearching cfSearching is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 117 cfSearching User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 16 m 2 sec
Reputation Power: 6
Quote:
Originally Posted by jimmydean101
String comparison issues: 0180 and 00180 are seen as EQ


The original code is not actually performing a string comparison. Remember CF is typeless. So it often does a lot of automatic conversion . CF probably converted the values "0180" and "00180" to numbers. As numbers those two values are equal. If you need to perform a strict string comparison, always use the compare() function.

Code:
<cfset value1 = "0180">
<cfset value2 = "00180">
<cfif compare(value1,value2) EQ 0>
	same
<cfelse>
	not same
</cfif>

Last edited by cfSearching : August 1st, 2011 at 01:05 PM.

Reply With Quote
  #7  
Old August 1st, 2011, 01:58 PM
kiteless kiteless is offline
Moderator
Dev Shed God (5000 - 5499 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 5,100 kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 4 h 29 m 54 sec
Reputation Power: 966
Just to note that this is exactly what is happening. CF is typeless, so when it sees numbers, it converts them into numbers under the hood to work with them. Same goes for dates, times, etc.

Reply With Quote
  #8  
Old August 1st, 2011, 02:46 PM
cfSearching cfSearching is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 117 cfSearching User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 16 m 2 sec
Reputation Power: 6
.. and though this case is rather obvious, it is not always clear how CF will interpret certain values. So like I said, it is best to be explicit.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreColdFusion Development > String comparison issues: 0180 and 00180 are seen as EQ

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap