CFLib.org – Common Function Library Project

arrayCompare(LeftArray, RightArray)

Last updated September 23, 2004

author

Ja Carter

Version: 1 | Requires: CF5 | Library: DataManipulationLib

Description:
Recursive functions to compare structures and arrays. The root structure or array may contain nested structures and arrays.

Return Values:
Returns a boolean.

Example:

<cfset LeftStruct = structNew()>
<cfset LeftStruct.Email = "test@Test.com">
<cfset LeftStruct.Name = "test">
<cfset LeftStruct.Struct = structNew()>
<cfset LeftStruct.Struct.keys = arrayNew(1)>
<cfset LeftStruct.Struct.keys[1] = "1">
<cfset LeftStruct.Struct.keys[2] = "2">
<cfset LeftStruct.Array = arrayNew(1)>
<cfset LeftStruct.Array[1] = structNew()>
<cfset LeftStruct.Array[1].id = "1">
<cfset LeftStruct.Array[1].name = "test">
<cfset LeftStruct.Array[2] = "321">

<cfset RightStruct = structNew()>
<cfset RightStruct.Name = "test">
<cfset RightStruct.Email = "test@Test.com">
<cfset RightStruct.Struct = structNew()>
<cfset RightStruct.Struct.keys = arrayNew(1)>
<cfset RightStruct.Struct.keys[1] = "1">
<cfset RightStruct.Struct.keys[2] = "2">
<cfset RightStruct.Array = arrayNew(1)>
<cfset RightStruct.Array[1] = structNew()>
<cfset RightStruct.Array[1].id = "1">
<cfset RightStruct.Array[1].name = "test">
<cfset RightStruct.Array[2] = "321">

<cfdump var="#LeftStruct#">
<cfdump var="#RightStruct#">
<cfoutput>#structCompare(LeftStruct,RightStruct)#</cfoutput>

Parameters:

Name Description Required
LeftArray The first array. Yes
RightArray The second array. Yes

Full UDF Source:

/**
 * Recursive functions to compare arrays and nested structures.
 * 
 * @param LeftArray      The first array. (Required)
 * @param RightArray      The second array. (Required)
 * @return Returns a boolean. 
 * @author Ja Carter (ja@nuorbit.com) 
 * @version 1, September 23, 2004 
 */
function arrayCompare(LeftArray,RightArray) {
    var result = true;
    var i = "";
    
    //Make sure both params are arrays
    if (NOT (isArray(LeftArray) AND isArray(RightArray))) return false;
    
    //Make sure both arrays have the same length
    if (NOT arrayLen(LeftArray) EQ arrayLen(RightArray)) return false;
    
    // Loop through the elements and compare them one at a time
    for (i=1;i lte arrayLen(LeftArray); i = i+1) {
        //elements is a structure, call structCompare()
        if (isStruct(LeftArray[i])){
            result = structCompare(LeftArray[i],RightArray[i]);
            if (NOT result) return false;
        //elements is an array, call arrayCompare()
        } else if (isArray(LeftArray[i])){
            result = arrayCompare(LeftArray[i],RightArray[i]);
            if (NOT result) return false;
        //A simple type comparison here
        } else {
            if(LeftArray[i] IS NOT RightArray[i]) return false;
        }
    }
    
    return true;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson