CFLib.org – Common Function Library Project

structCompare(LeftStruct, RightStruct)

Last updated October 14, 2005
Download UDF

author

Ja Carter                                         Ja Carter

Version: 2 | Requires: ColdFusion 5 | Library: DataManipulationLib

 
Rated 0 time(s). Average Rating: 0

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:

<cfscript>
/**
* 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;
}
</cfscript>

Search CFLib.org


Latest Additions

Shawn Porter Shawn Porter added
DeMoronize
3 hour(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
1 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
3 day(s) ago

Frank Marion Frank Marion added
sitemapPing
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson