StructOfArraysToStructOfStructs(struct, theKey[, cols])
Last updated August 02, 2003
Version: 1 | Requires: CF5 | Library: DataManipulationLib
Description:
Converts a structure of arrays to a keyed structure of structs. In other words - taking a struct where each key is an array, it converts the struct into a struct of structs, where a particular key is used as the root key.
Return Values:
Returns a struct.
Example:
<cfscript>
mystruct = structnew();
mystruct.name = listtoarray("Joe,Bob,Jim");
mystruct.weight = listtoarray("140,180,160");
mystruct.Age = listtoarray("25,40,33");
mystruct.ID = listtoarray("1,2,3");
</cfscript>
Original:
<cfdump var="#mystruct#">
<p>
New Structure:
<cfset new = StructOfArraysToStructOfStructs(mystruct,"ID")>
<cfdump var="#new#">
Parameters:
Name | Description | Required |
---|---|---|
struct | Struct to examine. | Yes |
theKey | Key in structure to use as new primary key. | Yes |
cols | Keys from original structure to use. Defaults to all. | No |
Full UDF Source:
/**
* Converts a structure of arrays to a keyed structure of structs.
*
* @param struct Struct to examine. (Required)
* @param theKey Key in structure to use as new primary key. (Required)
* @param cols Keys from original structure to use. Defaults to all. (Optional)
* @return Returns a struct.
* @author Casey Broich (cab@pagex.com)
* @version 1, August 2, 2003
*/
function StructOfArraysToStructOfStructs(struct,thekey){
var i = "";
var ii = "";
var new = structNew();
var value = "";
var cols = "";
if(arrayLen(arguments) GT 2) cols = listToArray(arguments[3]);
else cols = structkeyarray(struct);
for(i = 1; i lte arraylen(struct[thekey]); i = i + 1){
new[struct[thekey][i]] = structNew();
for(ii = 1; ii lte arraylen(cols); ii = ii + 1){
if(structKeyExists(struct,cols[ii])){
value = struct[cols[ii]][i];
}else{
value = "";
}
new[struct[thekey][i]][cols[ii]] = value;
}
}
return new;
}
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