CFLib.org – Common Function Library Project

DeepStructCount(myStruct)

Last updated August 23, 2002

author

Galen Smallen

Version: 2 | Requires: CF5 | Library: DataManipulationLib

Description:
I needed a way to count the number of keys in a structure that contained other structures. This UDF will parse all the way through and return the count. So far I have tested it to 5 nested deep. If an array is encountered, each element in the array will be tested to see if it is a structure. (However, the UDF still requires that the top level value be a structure.) Note - the count will not include a key that is a structure.

Return Values:
Returns a numeric value.

Example:

<cfset x = structNew()>
<cfset x.firstname = "ray">
<cfset x.lastname = "camden">
<cfset x.child = arrayNew(1)>
<cfset x.child[1] = structNew()>
<cfset x.child[1].firstName = "jacob">
<cfset x.child[1].lastName = "camden">
<cfset x.child[2] = structNew()>
<cfset x.child[2].firstName = "lynn">
<cfset x.child[2].lastName = "camden">

<cfoutput>
#deepstructcount(x)#
</cfoutput>

Parameters:

Name Description Required
myStruct The structure to examine. Yes

Full UDF Source:

/**
 * Counts the number of keys in a structure of structures.
 * Added missing return statement (rkc)
 * 
 * @param myStruct 	 The structure to examine. (Required)
 * @return Returns a numeric value. 
 * @author Galen Smallen (galen@oncli.com) 
 * @version 2, August 23, 2002 
 */
function deepStructCount(myStruct) {
    var deepCount=0;
    var x = "";
    var i = "";
        
    for (x in myStruct) { 
        if(isArray(myStruct[x])) {
            for(i=1; i lte arrayLen(myStruct[x]); i=i+1) {
                if(isStruct(myStruct[x][i])) deepCount = deepCount+deepStructCount(myStruct[x][i]);
            }
        } else if (isStruct(myStruct[x])) {
            deepCount=deepCount+deepStructCount(myStruct[x]);
        } else {
            deepCount=deepCount+1;
        }
    }
	return deepCount;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson