CFLib.org – Common Function Library Project

DeepStructCount(myStruct)

Last updated August 23, 2002

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

 
Rated 0 time(s). Average Rating: 0

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:

view plain print about
<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:

view plain print about
<cfscript>
/**
 * 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;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson