Using CFML (ColdFusion Markup Langauge, aka ColdFusion), how can you compare if two single dimension arrays are the same?
There's a very simple way of comparing two arrays using CFML's underlying java. According to a recent blog by Rupesh Kumar of Adobe (http://coldfused.blogspot.com/), ColdFusion arrays are an implementation of java lists (java.util.List). So all the Java list methods are available for CFML arrays.
So to compare 2 arrays all you need to do is use the equals method. It returns a YES if the arrays are equal and NO if they are not.
<cfset array1 = listToArray("tom,dick,harry,phred")/>
<cfset array2 = listToArray("dick,harry,phred") />
<cfset array3 = listToArray("tom,dick,harry,phred")/>
<cfoutput>
Array2 equals Array1 #array2.equals(array1)# (returns a NO) <br/>
Array3 equals Array1 #array3.equals(array1)# (returns a YES) <br/>
</cfoutput>
To build on James' answer, I thought that JSON might be preferrable over WDDX. In fact, it proves to be considerably more efficient. Comparing hashes is not that expensive, but serializing the data and then generating the hash could be (for larger and/or more complex data structures).
<cfsilent>
<!--- create some semi-complex test data --->
<cfset data = StructNew() />
<cfloop from="1" to="50" index="i">
<cfif variables.i mod 2 eq 0>
<cfset variables.data[variables.i] = StructNew()/>
<cfset tmp = variables.data[variables.i] />
<cfloop from="1" to="#variables.i#" index="j">
<cfset variables.tmp[variables.j] = 1 - variables.j />
</cfloop>
<cfelseif variables.i mod 3 eq 0>
<cfset variables.data[variables.i] = ArrayNew(1)/>
<cfset tmp = variables.data[variables.i] />
<cfloop from="1" to="#variables.i#" index="j">
<cfset variables.tmp[variables.j] = variables.j mod 6 />
</cfloop>
<cfset variables.data[variables.i] = variables.tmp />
<cfelse>
<cfset variables.data[variables.i] = variables.i />
</cfif>
</cfloop>
</cfsilent>
<cftimer label="JSON" type="inline">
<cfset jsonData = serializeJson(variables.data) />
<cfset jsonHash = hash(variables.jsonData) />
<cfoutput>
JSON: done.<br />
len=#len(variables.jsonData)#<br/>
hash=#variables.jsonHash#<br />
</cfoutput>
</cftimer>
<br/><br/>
<cftimer label="WDDX" type="inline">
<cfwddx action="cfml2wddx" input="#variables.data#" output="wddx" />
<cfset wddxHash = hash(variables.wddx) />
<cfoutput>
WDDX: done.<br />
len=#len(variables.wddx)#<br/>
hash=#variables.wddxHash#<br />
</cfoutput>
</cftimer>
Here's the output that the above code generates on my machine:
JSON: done.
len=7460
hash=5D0DC87FDF68ACA4F74F742528545B12
JSON: 0ms
WDDX: done.
len=33438
hash=94D9B792546A4B1F2FAF9C04FE6A00E1
WDDX: 47ms
While the data structure I'm serializing is fairly complex, it could easily be considered small. This should make the efficiency of JSON serialization over WDDX even more preferrable.
At any rate, if I were to try to write a "compareAnything" method using hash comparison, I would use JSON serialization over WDDX.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With