DeepStructCount(myStruct)
Last updated August 23, 2002
Version: 2 | Requires: ColdFusion 5 | 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.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:
<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>
Search CFLib.org
Latest Additions
Dave Anderson added
iniToStruct
20 day(s) ago
Dave Anderson added
deDupeArray
20 day(s) ago
Richard added
dice
22 day(s) ago
Isaac Dealey added
getRelative
a while ago
Top Rated
backupDatabase
Rated 5.0, 22 time(s)
indentXml
Rated 5.0, 10 time(s)
generateSsccAsn
Rated 5.0, 4 time(s)
highlightAndCrop
Rated 5.0, 4 time(s)