Skip navigation
Speedtossi
Currently Being Moderated

Comparison of strings

Feb 21, 2012 1:08 AM

Tags: #coldfusion8

Hello,

 

I have a problem with the comparison of strings:

If I compare the values 0,5 and 5,0 I will get the result that the values are equal (Case 1).

If I compare the values 0.5 and 5.0 the comparison will be correct (Case 2).

If I compare the values with the method 'compare()' the comparison is correct (Case 3).

 

Why is the result of case 1 wrong? Is there an explanation or is this a bug?

 

<cfscript>

    // wrong comparsion

    writeoutput("Case 1: comparsion with 'if' and '==' <br>");

    string_one = "0,5";

    string_two = "5,0";

 

    writeoutput("values: <font color=green>String one: #String_one#</font> - <font color=red> String Two: #string_two#</font><br><br>");

    if (string_one == string_two) {

        writeoutput("wrong result after the comparsion: #string_one# is not #string_two#<br><br>");

    }

    else {

        writeoutput("right result after the comparsion: #string_one# is not #string_two#<br><br>");

    }

   

    writeoutput("===============================<br>");

   

    // right comparsion

    writeoutput("Case 2: comparsion with 'if' and '==' <br>");

    string_three = "0.5";

    string_four = "5.0";

 

    writeoutput("values: <font color=green>String three: #String_three#</font> - <font color=red> String four: #string_four#</font><br><br>");

    if (string_three == string_four) {

        writeoutput("wrong result after the comparsion: #string_three# is not #string_four#<br><br>");

    }

    else {

        writeoutput("right result after the comparsion: #string_three# is not #string_four#<br><br>");

    }   

    writeoutput("===============================<br>");

   

    // right comparsion

    writeoutput("Case 3: comparsion with 'compare' <br>");

    string_five = "0,5";

    string_six = "5,0";

   

    writeoutput("values: <font color=green>String five: #String_five#</font> - <font color=red> String six: #string_six#</font><br><br>");

    if (compare(string_five,string_six) EQ 0){

        writeoutput("wrong result after the comparsion: #string_five# is not #string_six#<br><br>");

    }

    else{

        writeoutput("right result after the comparsion: #string_five# is not #string_six#<br><br>");

    }

 

    writeoutput("===============================<br>");

   

    writeoutput('</strong>');

</cfscript>

 
Replies
  • Currently Being Moderated
    Feb 21, 2012 1:41 AM   in reply to Speedtossi

    All of this can be somewhat simplified to this:

     

     

    <cfif "0,6" eq "6,0">

              EQUALS

    <cfelse>

              NOT EQUALS

    </cfif>

    And the answer is - according to CF - "EQUALS".

     

    My reaction to that is:

     

    WTF?

     

    Not much help, sorry.

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 21, 2012 1:42 AM   in reply to Adam Cameron.

    I'd imagine it's evaluating it as a list, and the two lists are the same despite ordering.

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 21, 2012 1:48 AM   in reply to Owain North

    It only "works" with (single) digits.

     

    <cfif "0,A" eq "A,0">

              EQUALS

    <cfelse>

              NOT EQUALS

    </cfif>

     

    and

     

    <cfif "0,66" eq "66,0">

              EQUALS

    <cfelse>

              NOT EQUALS

    </cfif>

     

    both output "NOT EQUALS".

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 21, 2012 4:53 AM   in reply to Adam Cameron.

    Someone from Adobe has explained this to me.  Believe it or not, both "0,5" and "5,0" - to CF - mean 0th May 2012... which equates to 30 April 2012 ("of course" we all cry, whilst slapping our foreheads ;-).

     

    Example:

    <cfset d1 = "0,5">

    <cfset d2 = "5,0">

     

    <cfoutput>

              #dateFormat(d1)#<br />

              #dateFormat(d2)#<br />

    </cfoutput>

     

    Sometimes I am still astounded by CF.

     

    Not in a good way.

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 21, 2012 4:55 AM   in reply to Adam Cameron.

    That is so weak, and is the kind of thing that's making me lean away from CF and towards strongly-typed languages like C#. That's just stupid.

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 21, 2012 5:00 AM   in reply to Owain North

    That is so weak, and is the kind of thing that's making me lean away from CF and towards strongly-typed languages like C#. That's just stupid.

     

    That's a polite version of what I said to the Adobe chap (not that it was his fault, but he was the one in my line of fire at the time.  I shoot the messenger on a regular basis too ;-).

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 21, 2012 5:34 AM   in reply to Owain North

    Owain North wrote:

     

    I'd imagine it's evaluating it as a list, and the two lists are the same despite ordering.

    Difficult for us to sell, isn't it?

     

    Adam Cameron wrote:

     

    Someone from Adobe has explained this to me.  Believe it or not, both "0,5" and "5,0" - to CF - mean 0th May 2012... which equates to 30 April 2012 ("of course" we all cry, whilst slapping our foreheads ;-).

    Does indeed make the eyes water.

     

    Speedtossi wrote:

     

    I have a problem with the comparison of strings:

    If I compare the values 0,5 and 5,0 I will get the result that the values are equal (Case 1).

     

    It gets even better.

     

    <cfif "0,0000005" eq "5,0">

    EQ

    <cfelse>

    NEQ

    </cfif>

     

    gave me EQ.

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 21, 2012 6:01 AM   in reply to Speedtossi

    Speedtossi,

    Referring to the news Adam brought from Adobe, I was also surprised when I first found out that isDate("0a") and isDate("3p") both return Yes. I suppose the moral is, when comparing strings, use compare or compareNoCase. When comparing numerical values, don't surround them with quotes.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 9, 2012 5:09 AM   in reply to Adam Cameron.

    I have raised this as a bug.

     

    https://bugbase.adobe.com/index.cfm?event=bug&id=3134331

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 10, 2012 1:45 PM   in reply to Speedtossi

    I prefer strong typed language, like Java.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 22, 2012 2:25 PM   in reply to ffcai

    Came across this while trying to find a solution wondering if anybody can point me in the right direction

     

    I have two lists, let's say

     

    list1="a,b,c,d"

    list2="b,c,d,a"

     

    Is there a quick and easy way to compare the two lists and see if they have exactly the same elements or differ in some way, I don't need to know how they differ, just that they are indeed different, order is not relevant to me either, just the values in the list.

     

    Using COMPARE does not work as it just seems to look at it as a string rather than a list

     

    Without getting into loops and code, is there a command for this (I am using CF8)

     

    Btw Just to clarify list1 would be a #valuelist()# from a SQL query, the other one would already exists as a hard coded list <CFSET list2="">

     

     

    Thanks

     

    Mark

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 22, 2012 8:04 PM   in reply to ACS LLC

    There is no command.  You have to think it through and write something appropriate for your own situation.    

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 22, 2012 9:13 PM   in reply to ACS LLC

    Would this suit your purposes?

     

    <cfset list1 = "a,b,c,d" />

    <cfset list2 = "b,c,d,a" />

     

    <cfif compare( listSort(list1, "text"), listSort(list2, "text") ) EQ 0>

    They match

    <cfelse>

    They don't match

    </cfif>

     

    jason

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 22, 2012 9:18 PM   in reply to 12Robots

    I was just about to post

     

    I took a step back and made sure that the hard coded list was always sorted, and also applied an ORDER to the SQL statement, then when the hard coded one was recreated using a script that creates a script I made sure it was sorted, so the ORDERED SQL would always match, that way I was indeed able to utilize a straight forward EQUALS command.

     

    I am surprised that CF does not have a tag that allows for list comparison in the same way a string can be compared.

     

    Thanks for the input

     

    Mark

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 23, 2012 1:31 AM   in reply to ACS LLC

    I am surprised that CF does not have a tag that allows for list comparison in the same way a string can be compared.

     

     

    Well you can use any string function you like on a list: lists are just strings with a notion of an element delimiter after all.  So to test two lists for equality, one can use the compare() or compareNoCase() functions.  Or in most situations just the EQ operator will suffice.

     

    However CF lists are ordered, so if you want to implement the concept of an "unordered list" (which is a completely legitimate notion), then it's also up to you to implement the functionality to deal with them.

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 23, 2012 2:01 AM   in reply to ffcai

    ffcai wrote:

     

    I prefer strong typed language, like Java.

    Then you want the coffeeshop round the corner.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 23, 2012 5:09 AM   in reply to Adam Cameron.

    CF lists are ordered? 

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 23, 2012 8:13 AM   in reply to Dan Bracuk

    In my case the list is not ordered, or certainly was no initially ordered, it was originally manually created and hard coded, but it was being overwritten when required by CF and recreated, so I just had to use ORDER in the SQL so that when I came back to do the compare it was the same as the next ORDER SQL.

     

    The only way that COMPARE would give the desired result on a list is if both lists were in the same order, as robots12 above pointed out, you need to sort them both

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points