How can you test to see if two arrays are the same?
CF does not offer an easy way to compare two single dimension arrays. The most common solution involving looping through one array and comparing each item to the second array. It is a complicated and messy solution.
Actually there's a very simple way of comparing two arrays using CF'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 CF 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.
For example, given these arrays:
<cfset array1 = listToArray("tom,dick,harry,phred")/> <cfset array2 = listToArray("dick,harry,phred") /> <cfset array3 = listToArray("tom,dick,harry,phred")/>
To test for equality:
<cfoutput> Array2 equals Array1 #array2.equals(array1)# (returns a NO) <br/> Array3 equals Array1 #array3.equals(array1)# (returns a YES) <br/> </cfoutput>
This question was written by Larry C. Lyons
It was last updated on July 21, 2008.