Extending ColdFusion's List Functions: Append or Prepend Individual List Items

Recently, I ran into a situation where I was typing out a bunch of cfinvokeargument tags for a long list of variables and when I began typing essentially the same information over and over again I thought "I could probably do this more efficiently!" So, I threw together a fairly simple user defined function (UDF) that I wrapped up into a ColdFusion component (.cfc).

The theory is fairly simple. Take a list, loop over each list item (or element) and prepend each individual list item with a predefined value.

For example, if a list contains the following values: (ItemName,ItemNumber,Quantity,PaymentDate). You could prepend each list item with "request." thus receiving a new list containing: (request.ItemName,request.ItemNumber,request.Quantity,request.PaymentDate).

This varies from ColdFusion's built in functions ListAppend and ListPrepend in that the built in functions add list items (or elements) to a list, but do not actually modify the list items themselves. Whereas I wanted to modify each individual list item or element, not necessarily add new list items at the beginning or end of the list.

While I was at it, I went ahead and threw in a method to append list items/elements as well. Hope this saves time for someone else too.

Here's the ColdFusion component:


<!---------------------------------------------------------------------------------------------------------------

    Document:        /extensions/components/listFunctionExtensions.cfc
    Author:            Steve Withington (http://www.stephenwithington.com)
    Creation Date:    12/22/2008
    Copyright:        (c) 2008 Stephen J. Withington, Jr. | stephenwithington.com

    License:        This code may be used freely.
                    You may modify this code as you see fit, however, this header must remain intact.
                    
                    This code is provided as is. I make no warranty or guarantee.
                    Use of this code is at your own risk.
    
    Purpose:        Prepend (or append) a value to the beginning (or end) of each item in a list.
    
                    For example, if a list contains the following values:
                    (ItemName,ItemNumber,Quantity,PaymentDate)
                    you could prepend each list item with "request." thus receiving a new list containing
                    (request.ItemName,request.ItemNumber,request.Quantity,request.PaymentDate).
                    This could easily be modified to allow for custom delimiters, etc. but
                    I wanted to keep this simple.

    Example Usage:
    http://www.stephenwithington.com/blog/index.cfm/2008/12/22/Extending-ColdFusions-List-Functions-Append-or-Prepend-Individual-List-Items
    
    Revision Log:    
    MM/DD/2008 - sjw - notes.

---------------------------------------------------------------------------------------------------------------->

<cfcomponent     displayname="List Function Extensions"
                output="false"
                hint="PrependListItems() and AppendListItems() methods.">


    <!--- prependListItems(currentList, prependValue) --->
    <cffunction name="prependListItems"
                access="public"
                returntype="string"
                output="false">


        <cfargument name="currentList"
                    type="string"
                    required="yes" />


        <cfargument name="prependValue"
                    type="string"
                    required="yes" />


        <cfset var listResult = "" />
        <cfset var listToPrepend = arguments.currentList />
        <cfset var theValue = arguments.prependValue />
        <cfset var tempArray = arrayNew(1) />

        <cfloop list="#listToPrepend#" index="i">
            <cfset temp = arrayAppend(tempArray, theValue & i) />
        </cfloop>

        <cfset listResult = arrayToList(tempArray, ",") />

        <cfreturn listResult />

    </cffunction>


    <!--- appendListItems(currentList, appendValue) --->
    <cffunction name="appendListItems"
                access="public"
                returntype="string"
                output="false">


        <cfargument name="currentList"
                    type="string"
                    required="yes" />


        <cfargument name="appendValue"
                    type="string"
                    required="yes" />


        <cfset var listResult = "" />
        <cfset var listToAppend = arguments.currentList />
        <cfset var theValue = arguments.appendValue />
        <cfset var tempArray = arrayNew(1)>

        <cfloop list="#listToAppend#" index="i">
            <cfset temp = arrayAppend(tempArray, i & theValue) />
        </cfloop>

        <cfset listResult = arrayToList(tempArray, ",") />

        <cfreturn listResult />

    </cffunction>

</cfcomponent>

Here's an example of both the PrependListItems method and AppendListItems method:


<cfset tempList = "ItemName,ItemNumber,Quantity,PaymentDate" />
                    
<cfscript>
    args = structNew();
    args.currentList = tempList;
    args.prependValue = "request.";
</cfscript>

<!--- you may need to modify this to your placement of listFunctionExtensions.cfc --->
<cfinvoke     component="extensions.components.listFunctionExtensions"
            method="prependListItems"
            argumentcollection="#args#"
            returnvariable="myNewPrependedList" />


<ul>
    <cfloop list="#myNewPrependedList#" index="i">
        <cfoutput><li>#i# – #listlast(i, '.')#</li></cfoutput>
    </cfloop>
</ul>

<hr size="1" />

<cfscript>
    args = structNew();
    args.currentList = tempList;
    args.appendValue = "1";
</cfscript>

<cfinvoke     component="extensions.components.listFunctionExtensions"
            method="appendListItems"
            argumentcollection="#args#"
            returnvariable="myNewAppendedList" />


<ul>
    <cfloop list="#myNewAppendedList#" index="i">
        <cfoutput><li>#i#</li></cfoutput>
    </cfloop>
</ul>

© 2026, Stephen J. Withington, Jr.  |  Hosted by Hostek.com

Creative Commons License   |   This work is licensed under a Creative Commons Attribution 3.0 Unported License.