CFLib.org – Common Function Library Project

ListToStruct(list[, delimiter])

Last updated April 01, 2010

author

Rob Brooks-Bilson

Version: 2 | Requires: CF5 | Library: DataManipulationLib

Description:
Converts a delimited list of key/value pairs to a structure. This function gives you the equivalent of StructNew() with constructor support.

Return Values:
Returns a structure.

Example:

<CFSET MyList="key1=value1&key2=value2&key3=value3">
<CFSET x=ListToStruct(MyList, "&")>
<CFSET y=ListToStruct("a=1: b=2: c=3", ":")>

<CFDUMP VAR="#x#">
<CFDUMP VAR="#y#">

Parameters:

Name Description Required
list List of key/value pairs to initialize the structure with. Format follows key=value. Yes
delimiter Delimiter seperating the key/value pairs. Default is the comma. No

Full UDF Source:

/**
 * Converts a delimited list of key/value pairs to a structure.
 * v2 mod by James Moberg
 * 
 * @param list      List of key/value pairs to initialize the structure with.  Format follows key=value. (Required)
 * @param delimiter      Delimiter seperating the key/value pairs.  Default is the comma. (Optional)
 * @return Returns a structure. 
 * @author Rob Brooks-Bilson (rbils@amkor.com) 
 * @version 2, April 1, 2010 
 */
function listToStruct(list){
       var myStruct = StructNew();
       var i = 0;
       var delimiter = ",";
       var tempList = arrayNew(1);
       if (ArrayLen(arguments) gt 1) {delimiter = arguments[2];}
       tempList = listToArray(list, delimiter);
       for (i=1; i LTE ArrayLen(tempList); i=i+1){
               if (not structkeyexists(myStruct, trim(ListFirst(tempList[i], "=")))) {
                       StructInsert(myStruct, trim(ListFirst(tempList[i], "=")), trim(ListLast(tempList[i], "=")));
               }
       }
       return myStruct;
}

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

Created by Raymond Camden / Design by Justin Johnson