LenStruct(structIn[, excludeList][, ending])
Last updated October 05, 2004
Version: 1 | Requires: CF5 | Library: DataManipulationLib
Description:
Computes the length of every key in the passed structure. Optionally allows you to set the end stem of the returned struct. The return structure keys are the same as the passed structure keys plus the end stem is appended (default: "_Len"). Also, allows you to pass an exclude list of keys not to return if you wish to only get lens on some of the passed structure. This UDF will not process complex data types - simple values only. Great to use on the form scope (which is a stucture itself) if you do len checking for data validation passed from forms.
Feel free to modify if you wish to recursively go through nested structures.
Thanks to Mike Gillespie and Raymond Camden and their TrimStruct UDF for giving me this idea.
Return Values:
Returns a struct.
Example:
<cfscript>
// Create test struct and set vars
test = StructNew();
test.a = "a";
test.bb = "bb";
test.ccc = "ccc";
// With no optional ending or exludelist
test_len = LenStruct(test);
</cfscript>
<cfdump var="#test_len#">
Parameters:
Name | Description | Required |
---|---|---|
structIn | The struct to check. | Yes |
excludeList | List of keys to ignore. | No |
ending | String to append to key names in resulting struct. | No |
Full UDF Source:
/**
* Computes the length of every key in the passed structure and returns a structure with unique key names of the lengths.
*
* @param structIn The struct to check. (Required)
* @param excludeList List of keys to ignore. (Optional)
* @param ending String to append to key names in resulting struct. (Optional)
* @return Returns a struct.
* @author Peter J. Farrell (pjf@maestropublishing.com)
* @version 1, October 5, 2004
*/
function LenStruct(structIn) {
var i = 0;
var structIn_count = StructCount(structIn);
var struct0ut = StructNew();
var ending = "_Len";
var excludeList = "";
var key = "";
// Check if excludeList was passed
if(arrayLen(Arguments) GT 1) {
excludeList = Arguments[2];
}
// Check if different ending was passed
if(arrayLen(Arguments) GT 2) {
ending = Arguments[3];
}
for (key IN structIn) {
if (NOT listFindNoCase(excludeList,key) AND isSimpleValue(structIn[key])) {
structOut[key&ending] = Len(structIn[key]);
}
}
return structOut;
}
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