Comparing ColdFusion Struct Equality With Java

Posted August 21, 2006 at 6:15 PM

Tags: ColdFusion

I am really loving this whole Java thing. Have you ever wanted to compare two different Structs in ColdFusion? Maybe do something like:

 Launch code in new window » Download code as text file »

  • <!--- Set up one struct. --->
  • <cfset objGirlA = StructNew() />
  • <cfset objGirlA.Name = "Sarah" />
  • <cfset objGirlA.IsHottie = "Yes" />
  • <cfset objGirlA.Measurements = ListToArray( "36,28,36" ) />
  •  
  • <!--- Set up another struct. --->
  • <cfset objGirlB = StructNew() />
  • <cfset objGirlB.Name = "Sarah" />
  • <cfset objGirlB.IsHottie = "Yes" />
  • <cfset objGirlB.Measurements = ListToArray( "36,28,36" ) />
  •  
  • <!--- Test for equality. --->
  • <cfset blnIsEqual = (objGirlA EQ objGirlB) />

If you ever have, then you know this throws the following error:

Complex object types cannot be converted to simple values. The expression has requested a variable or an intermediate expression result as a simple value, however, the result cannot be converted to a simple value. Simple values are strings, numbers, boolean values, and date/time values. Queries, arrays, and COM objects are examples of complex values.

No such luck. However, if you grab the Java methods on ColdFusion structs, such a thing is indeed possible. Structs have the Java method, Equals():

 Launch code in new window » Download code as text file »

  • <cfset blnIsEqual = objGirlA.Equals( objGirlB ) />

This compares the passed struct, objGirlB, to the calling struct, objGirlA. And, you guessed it, it works just fine.

There are, however, some caveats. You can only compare simple values. Well, that's not exactly true; you can only compare values that are passed by value (not by reference), from what I can see. This includes numbers, strings, dates, and even arrays. In the above example, the Measurements key gets compared successfully even though it is an array and considered complex. If you have any keys that have components or Java objects, they are not compared; it doesn't throw an error, but I don't think any comparison is taking place. For instance, these two structs:

 Launch code in new window » Download code as text file »

  • <!--- Create one with hotness 10. --->
  • <cfset objGirlA = StructNew() />
  • <cfset objGirlA.Name = "Sarah" />
  • <cfset objGirlA.Hotness = CreateObject( "component", "Girl" ).Init(
  • hotness = 10
  • ) />
  •  
  • <!--- Create one with hotness 9. --->
  • <cfset objGirlB = StructNew() />
  • <cfset objGirlB.Name = "Sarah" />
  • <cfset objGirlB.Hotness = CreateObject( "component", "Girl" ).Init(
  • hotness = 9
  • ) />

 

... when compares are evaluated as the same. The differences in hotness (10 vs. 9) does not get taken into account.

Anyway, not sure how useful this is, but sort of nifty little thing.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

May 14, 2009 at 7:07 PM // reply »
1 Comments

Be careful when using this technique. There are some strange quirks with using equals() with ColdFusion numbers. For instance:

<cfset a = structNew() />
<cfset a.n = 2 />

<cfset b = structNew() />
<cfset b.n = 2 />

<cfoutput>
#a.equals(b)#
</cfoutput>

The above outputs "YES" as expected. But if we change b.n to 1+1:

<cfset a = structNew() />
<cfset a.n = 2 />

<cfset b = structNew() />
<cfset b.n = 1 + 1 />

<cfoutput>
#a.equals(b)#
</cfoutput>

it now outputs "NO"!


May 19, 2009 at 9:35 AM // reply »
6,516 Comments

@Tony,

Hmm, very interesting. I never ended up using this anywhere; but it's good to know about these issues. Thanks.


Sep 17, 2009 at 9:14 AM // reply »
5 Comments

This is a great tip, thanks! Was already thinking about first converting the structs to wddx, but that didn't feel right. Will use this a lot, I guess!


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »