CFLib.org – Common Function Library Project

StructOfListsToArrayOfStructs(struct[, delim][, cols])

Last updated August 2, 2003

author

Casey Broich

Version: 1 | Requires: CF5 | Library: DataManipulationLib

Description:
Converts a structure of Lists to an Array of structures.

Return Values:
Returns an array.

Example:

<cfscript>
  mystruct = structnew();
  mystruct.name = "Joe,Bob,Jim";
  mystruct.weight = "140,180,160";
  mystruct.Age = "25,40,33";
</cfscript>

<cfset aos = StructOfListsToArrayOfStructs(mystruct)>
<cfdump var="#aos#">

<cfset aos = StructOfListsToArrayOfStructs(mystruct,",","weight,age")>
<cfdump var="#aos#">

Parameters:

Name Description Required
struct Struct of lists. Yes
delim List delimiter. Defaults to a comma. No
cols Struct keys to use. Defaults to all. No

Full UDF Source:

/**
 * Converts a structure of Lists to an Array of structures.
 * 
 * @param struct 	 Struct of lists. (Required)
 * @param delim 	 List delimiter. Defaults to a comma. (Optional)
 * @param cols 	 Struct keys to use. Defaults to all. (Optional)
 * @return Returns an array. 
 * @author Casey Broich (cab@pagex.com) 
 * @version 1, August 2, 2003 
 */
function StructOfListsToArrayOfStructs(Struct){
   var delim = ",";
   var theArray = arraynew(1);
   var row = 1;
   var i = ""; 
   var cols = structkeyarray(Struct);
   var count = 0;
   var value = ""; 
   var strow = "";

   if(arrayLen(arguments) GT 1) delim = arguments[2];
   if(arrayLen(arguments) GT 2) cols = listToArray(arguments[3]);
   count = listlen(struct[cols[1]],delim);
   if(arraylen(cols) gt 0) {
      for(row=1; row LTE count; row = row + 1){
      strow = structnew();
      for(i=1; i lte arraylen(cols); i=i+1) {
         if(structKeyExists(Struct,cols[i])){
            if(listlen(Struct[cols[i]],delim) gte row){
               value = listgetat(Struct[cols[i]],row,delim);
            }else{
               value = "";
            }
         }else{
            value = "";
         }
         strow[cols[i]] = value;
      }
      arrayAppend(theArray,strow);
      }
   }
   return theArray;
}
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