CFLib.org – Common Function Library Project

ListSplit(inList, numGet[, inDelimiter][, outDelimiter])

Last updated March 11, 2010

author

Ryan Thompson-Jewell

Version: 3 | Requires: CF5 | Library: StrLib

Description:
A split function for lists, splitting the original list into lists of n array elements. Returns an array of the list segments.

Return Values:
Returns an array of lists.

Example:

<cfset form.invoices = "205106,205107,R66591,R66647 RT4036">
<cfset invoicesSplit = listsplit(inList="#form.invoices#", numGet="#numberformat(listLen(form.invoices)/2)#", inDelimiter=",", outDelimiter=" ")>
<cfdump var="#invoicesSplit#">

Parameters:

Name Description Required
inList The list to split. Yes
numGet Number of items per array element. Yes
inDelimiter List delimiter. Defaults to a comma. No
outDelimiter Output list delimiter, defaults to comma No

Full UDF Source:

<!---
 A split function for lists, splitting the original list into lists of n array elements.
 Rewritten by Raymond Camden
 Output delimiter mod by Jules Gravinese (webveteran.com), December 2009
 
 @param inList 	 The list to split. (Required)
 @param numGet 	 Number of items per array element. (Required)
 @param inDelimiter 	 List delimiter. Defaults to a comma. (Optional)
 @param outDelimiter 	 Output list delimiter, defaults to comma (Optional)
 @return Returns an array of lists. 
 @author Ryan Thompson-Jewell (ryan@thompson-jewell.com) 
 @version 3, March 11, 2010 
--->
<cffunction name="listSplit" output="no" returnType="array" description="
	* A split function for lists, splitting the original list into lists of n array elements.
	* Rewritten by Raymond Camden
	* Output delimiter mod by Jules Gravinese (webveteran.com), December 2009
	*
	* @author  Ryan Thompson-Jewell (ryan@thompson-jewell.com)
	* @version 2, September 24, 2002
">
	<cfargument name="inList"       default=""  hint="The list to split">
	<cfargument name="numGet"       default="1" hint="Number of items per array alement">
	<cfargument name="inDelimiter"  default="," hint="Input delimiter">
	<cfargument name="outDelimiter" default="," hint="Output delimiter">

    <cfscript>
    var aResult=arraynew(1);

    if(numGet gte listLen(inList,inDelimiter)) {
        aResult[1] = inList;
        return aResult;
    }
    aResult[1] = "";
    while(listLen(inList,inDelimiter)) {
        aResult[arrayLen(aResult)] = listAppend(aResult[arrayLen(aResult)],listFirst(inList,inDelimiter), outDelimiter);
        inList = listRest(inList,inDelimiter);
        if(listLen(aResult[arrayLen(aresult)],outDelimiter) is numGet and len(inList)) aResult[arrayLen(aResult)+1] = "";
    }
    return aResult;
    </cfscript>
</cffunction>
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