The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> ColdFusion Development
|
String comparison issues: 0180 and 00180 are seen as EQ
Discuss String comparison issues: 0180 and 00180 are seen as EQ in the ColdFusion Development forum on Dev Shed. String comparison issues: 0180 and 00180 are seen as EQ ColdFusion Development forum discussing CFML coding practices, tips on CFML, and other CFML related topics. Find out why ColdFusion is the tool of choice for many e-commerce developers.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 28th, 2011, 04:50 PM
|
|
Registered User
|
|
Join Date: Jul 2011
Posts: 3
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!
|

July 28th, 2011, 07:05 PM
|
|
Registered User
|
|
Join Date: Feb 2009
Posts: 7
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.
|

July 28th, 2011, 07:18 PM
|
|
Registered User
|
|
Join Date: Feb 2009
Posts: 7
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>
|

July 28th, 2011, 07:50 PM
|
|
Registered User
|
|
Join Date: Jul 2011
Posts: 3
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>
|

July 29th, 2011, 11:17 AM
|
|
Registered User
|
|
Join Date: Jul 2011
Posts: 3
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>
|

August 1st, 2011, 01:02 PM
|
|
Contributing User
|
|
Join Date: May 2008
Posts: 117
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.
|

August 1st, 2011, 01:58 PM
|
|
Moderator
|
|
Join Date: Jun 2002
Location: Raleigh, NC
|
|
|
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.
|

August 1st, 2011, 02:46 PM
|
|
Contributing User
|
|
Join Date: May 2008
Posts: 117
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|