CFLib.org – Common Function Library Project

ListLoop(theList, theEval[, theDelim])

Last updated June 26, 2002

author

Shawn Seley

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Applies the passed evaluation to every element in the specified list. Allows versatile conversions, calculations, formatting and other alterations to an entire list in one easy step. Use the variable "x" in the passed evaluation to denote the current list element.

Return Values:
Returns a string.

Example:

<cfset my_list = "one,two,three">
<cfset my_list2 = "1,2,3">
<cfset my_list3 = "This is a test">

<cfoutput>
#ListLoop(my_list, "x & ""y""")#<br>
<br>
#ListLoop(my_list, "Left(x, Len(x)-1)")#<br>
<br>
#ListLoop(my_list2, "x*3")#<br>
<br>
#ListLoop(my_list2, "NumberFormat(x, ""0.000"")")#<br>
<br>
#ListLoop(my_list2, "MonthAsString(x)")#<br>
<br>
#ListLoop(my_list3, "Reverse(x)", " ")#<br>
</cfoutput>

Parameters:

Name Description Required
theList The list to work with. Yes
theEval The evaluation to execute. Yes
theDelim Delimiter to use. Defaults to a comma. No

Full UDF Source:

/**
 * Applies simple evaluations to every element in a list.
 * 
 * @param theList 	 The list to work with. (Required)
 * @param theEval 	 The evaluation to execute. (Required)
 * @param theDelim 	 Delimiter to use. Defaults to a comma. (Optional)
 * @return Returns a string. 
 * @author Shawn Seley (shawnse@aol.com) 
 * @version 1, June 26, 2002 
 */
function ListLoop(theList, theEval) {
	var i             = 0;
	var theList_len   = 0;
	var x             = "";

	var theDelim      = ",";
	if(ArrayLen(Arguments) GTE 3) theDelim = Arguments[3];

	theList_len   = ListLen(theList, theDelim);

	for (i=1; i LTE theList_len; i=i+1) {
		x = ListGetAt(theList, i, theDelim);
		theList = ListSetAt(theList, i, Evaluate(theEval), theDelim);
	}

	return theList;
}
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