• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Comparison of strings

New Here ,
Feb 21, 2012 Feb 21, 2012

Copy link to clipboard

Copied

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>

Views

3.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 21, 2012 Feb 21, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Feb 21, 2012 Feb 21, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 21, 2012 Feb 21, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 21, 2012 Feb 21, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 21, 2012 Feb 21, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Feb 21, 2012 Feb 21, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 21, 2012 Feb 21, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 21, 2012 Feb 21, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 10, 2012 Mar 10, 2012

Copy link to clipboard

Copied

I prefer strong typed language, like Java.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 22, 2012 Mar 22, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 22, 2012 Mar 22, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 22, 2012 Mar 22, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 22, 2012 Mar 22, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 23, 2012 Mar 23, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 23, 2012 Mar 23, 2012

Copy link to clipboard

Copied

CF lists are ordered? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 23, 2012 Mar 23, 2012

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 23, 2012 Mar 23, 2012

Copy link to clipboard

Copied

ffcai wrote:

I prefer strong typed language, like Java.

Then you want the coffeeshop round the corner.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation