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

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson