CFLib.org – Common Function Library Project

ListToStruct(list[, delimiter])

Last updated April 1, 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;
}
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