CFLib.org – Common Function Library Project

ListToStructRepeatKeys(list[, delimiter])

Last updated August 3, 2005

author

Tony Brandner

Version: 2 | Requires: CF5 | Library: DataManipulationLib

Description:
Based on ListToStruct() from Rob Brooks-Bilson, this one allows the structure key to be repeated. In the case of multiple values for a particular key, a comma-delimited list is created. Useful for parsing strings such as LDAP DNs, which may contain multiple values for a single key.

Return Values:
Returns a struct.

Example:

<cfset tempIn = "a=1;b=2;b=3;c=4">
<cfdump var="#ListToStructRepeatKeys(tempIn,";")#">

Parameters:

Name Description Required
list List of key and value pairs. Yes
delimiter List delimiter. Defaults to a comma. No

Full UDF Source:

/**
 * Based on ListToStruct() from Rob Brooks-Bilson, this one allows the structure key to be repeated and the value added to a list.
 * Version 2 - Ray modified the code a bit and fixed a missing var.
 * 
 * @param list 	 List of key and value pairs. (Required)
 * @param delimiter 	 List delimiter. Defaults to a comma. (Optional)
 * @return Returns a struct. 
 * @author Tony Brandner (tony@brandners.com) 
 * @version 2, August 3, 2005 
 */
function listToStructRepeatKeys(list){
  var myStruct=StructNew();
  var i=0;
  var delimiter=",";
  var tempVar="";

  if(arrayLen(arguments) gt 1) delimiter = arguments[2];

  for(i=listLen(list, delimiter); i gt 0; i=i-1) {
  	tempVar = trim(listGetAt(list, i, delimiter));
  	if (structKeyExists(myStruct,listFirst(tempVar, "="))) {
		myStruct[listFirst(tempVar, "=")] = listAppend(myStruct[listFirst(tempVar, "=")],listLast(tempVar, "="));
	} else {
		myStruct[listFirst(tempVar, "=")] = listLast(tempVar, "=");
	}
  }
  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