CFLib.org – Common Function Library Project

ListMid(list, startPos, numElements [, delimiter])

Last updated April 11, 2002
Download UDF

author

Rob Brooks-Bilson                                 Rob Brooks-Bilson

Version: 1 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 0 time(s). Average Rating: 0

Description:
A Mid() function for lists. Returns n elements starting at the specified start position. Accepts an optional delimiter. Note that if the number of elements to return is greater than the number of elements in the list, the UDF simply returns all elements.

Return Values:
Returns a string.

Example:

<CFSET List="1,2,3,4,5,6,7,8,9,10">
<CFSET List2="a;b;c;d;e;f;g;h">

<CFOUTPUT>
#ListMid(List, 3, 5)#<BR>
#ListMid(List, 3, 99)#<BR>
#ListMid(List2, 2, 16, ";")#<BR>
</CFOUTPUT>

Parameters:

Name Description Required
list List you want to return the elements from. Yes
startPos Starting position in the list to begin returning from. Yes
numElements Number of elements you want returned. Yes
delimiter Delimiter for the list. Default is the comma. No

Full UDF Source:

<cfscript>
/**
* A Mid() function for lists. Returns n elements starting at the specified start position.
* 3/25/02: Removed a line of extraneous code.
* 4/11/02: Fixed problem with going past end of list.
*
* @param list      List you want to return the elements from.
* @param startPos      Starting position in the list to begin returning from.
* @param numElements      Number of elements you want returned.
* @param delimiter      Delimiter for the list. Default is the comma.
* @return Returns a string.
* @author Rob Brooks-Bilson (rbils@amkor.com)
* @version 1.1, April 11, 2002
*/

function ListMid(list, startPos, numElements){
var tempList="";
var i=0;
var delimiter=",";
var finish = startPos+numElements;
if (ArrayLen(arguments) gt 3)
delimiter = arguments[4];
if (startPos+numElements gt ListLen(list, delimiter))
finish = ListLen(list, delimiter)+1;
for (i=startPos; i LT finish; i=i+1){
tempList=ListAppend(tempList, ListGetAt(list, i, delimiter), delimiter);
}
return tempList;
}
</cfscript>

Search CFLib.org


Latest Additions

Shawn Porter Shawn Porter added
DeMoronize
3 hour(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
1 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
3 day(s) ago

Frank Marion Frank Marion added
sitemapPing
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson