#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2011
    Posts
    3
    Rep 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!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    7
    Rep 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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    ToString()


    You don't even have to use ToString()

    <cfscript>
    num1 = 0081;
    num2 = 0081;
    comparison = Compare(num2,num1);
    WriteOutput(comparison);
    </cfscript>
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2011
    Posts
    3
    Rep 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>
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2011
    Posts
    3
    Rep 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>
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    131
    Rep Power
    7
    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.
  12. #7
  13. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,278
    Rep Power
    968
    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.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    131
    Rep Power
    7
    .. 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.

IMN logo majestic logo threadwatch logo seochat tools logo