CFLib.org – Common Function Library Project

structCompare(LeftStruct, RightStruct)

Last updated October 14, 2005

author

Ja Carter

Version: 2 | 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
LeftStruct The first struct. Yes
RightStruct The second structure. Yes

Full UDF Source:

/**
 * Recursive functions to compare structures and arrays.
 * Fix by Jose Alfonso.
 * 
 * @param LeftStruct      The first struct. (Required)
 * @param RightStruct      The second structure. (Required)
 * @return Returns a boolean. 
 * @author Ja Carter (ja@nuorbit.com) 
 * @version 2, October 14, 2005 
 */
function structCompare(LeftStruct,RightStruct) {
    var result = true;
    var LeftStructKeys = "";
    var RightStructKeys = "";
    var key = "";
    
    //Make sure both params are structures
    if (NOT (isStruct(LeftStruct) AND isStruct(RightStruct))) return false;

    //Make sure both structures have the same keys
    LeftStructKeys = ListSort(StructKeyList(LeftStruct),"TextNoCase","ASC");
    RightStructKeys = ListSort(StructKeyList(RightStruct),"TextNoCase","ASC");
    if(LeftStructKeys neq RightStructKeys) return false;    
    
    // Loop through the keys and compare them one at a time
    for (key in LeftStruct) {
        //Key is a structure, call structCompare()
        if (isStruct(LeftStruct[key])){
            result = structCompare(LeftStruct[key],RightStruct[key]);
            if (NOT result) return false;
        //Key is an array, call arrayCompare()
        } else if (isArray(LeftStruct[key])){
            result = arrayCompare(LeftStruct[key],RightStruct[key]);
            if (NOT result) return false;
        // A simple type comparison here
        } else {
            if(LeftStruct[key] IS NOT RightStruct[key]) 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