CFLib.org – Common Function Library Project

ListLeft(list, numElements [, delimiter])

Last updated April 24, 2002

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

 
Rated 1 time(s). Average Rating: 5.0

Description:
A Left() function for lists. Returns the n leftmost elements from the specified list. 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:

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

<CFOUTPUT>
#ListLeft(List, 3)#<BR>
#ListLeft(List2, 50)#
</CFOUTPUT>

Parameters:

Name Description Required
list List you want to return the n leftmost elements from. Yes
numElements Number of leftmost elements you want returned. Yes
delimiter Delimiter for the list. Default is the comma. No

Full UDF Source:

view plain print about
<cfscript>
/**
 * A Left() function for lists.  Returns the n leftmost elements from the specified list.
 * 
 * @param list      List you want to return the n leftmost elements from. 
 * @param numElements      Number of leftmost 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, April 24, 2002 
 */

function ListLeft(list, numElements){
  var tempList="";
  var i=0;
  var delimiter=",";
  if (ArrayLen(arguments) gt 2){
    delimiter = arguments[3];
  }
  if (numElements gte ListLen(list, delimiter)){
    return list;
  }
  for (i=1; i LTE numElements; i=i+1){
    tempList=ListAppend(tempList, ListGetAt(list, i, delimiter), delimiter);
  }
  return tempList;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Troy Pullis Troy Pullis added
firstXDayOfMonth
22 day(s) ago

Henry Ho Henry Ho added
arrayMap
a while ago

Henry Ho Henry Ho added
queryGetRow
a while ago

Tony Felice Tony Felice added
getRowFromQuery
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 44 time(s)

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 8 time(s)

Markus Schneebeli                                 ListRemoveByStri...
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson