Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you test to see if two arrays are the same using CFML?

Tags:

coldfusion

Using CFML (ColdFusion Markup Langauge, aka ColdFusion), how can you compare if two single dimension arrays are the same?

like image 457
Jason Avatar asked Nov 29 '22 21:11

Jason


2 Answers

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>
like image 117
Jason Avatar answered Dec 10 '22 04:12

Jason


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.

like image 43
Adam Tuttle Avatar answered Dec 10 '22 03:12

Adam Tuttle