CFLib.org – Common Function Library Project

saTOss(struct, theKey[, cols])

Last updated June 12, 2003

author

Casey Broich

Version: 1 | Requires: CF5 | Library: DataManipulationLib

Description:
Converts a structure of arrays into a name/key style structure. For example, if the structure contains three keys, each arrays, named age, ID, and name, you can ask this UDF to return a new structure where the keys are based on ID. Each item in the ID array will be a key of the new struct. Each key will be a struct containing each of the keys from the previous structure, along with the corresponding data. In other words, oldStruct.name[X] will equal newStruct[id].name. Optional param to specify which columns each keyed structure will contain.

Return Values:
Returns a structure.

Example:

<cfscript>
mystruct = structnew();
mystruct.ID = listtoarray("400,100,500");
mystruct.Name = listtoarray("Joe,John,Jake");
mystruct.Age = listtoarray("35,25,29");
</cfscript>
<cfset myStruct = saTOss(mystruct,"ID")>
<cfdump var="#myStruct#">

Parameters:

Name Description Required
struct Structure to convert. Yes
theKey Struct key used to define new struct. Yes
cols Keys to include in new structure. No

Full UDF Source:

/**
 * Converts a structure of arrays into a name/key style structure.
 * 
 * @param struct 	 Structure to convert. (Required)
 * @param theKey 	 Struct key used to define new struct. (Required)
 * @param cols 	 Keys to include in new structure. (Optional)
 * @return Returns a structure. 
 * @author Casey Broich (cab@pagex.com) 
 * @version 1, June 12, 2003 
 */
function saTOss(struct,thekey){
	var x = "";
	var i = ""; 
	var ii = ""; 
	var new = structnew();
	var cols = structkeyarray(Struct); 

	if(arrayLen(arguments) GT 2) cols = listToArray(arguments[3]);
	
	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){
			new[Struct[thekey][i]][cols[ii]] = Struct[cols[ii]][i];
		}
	}
	return new;
}
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