ColdFusion Lists

A Coldfusion list (or any list for that matter) is simply a string. The thing that makes this string different (to any other string) is that it contains "delimiters'. Delimiters (sometimes referred to as "separators") are used to separate each item in the list.

For example: ColdFusion Lists,ColdFusion Arrays,ColdFusion Structures could be a "comma delimited", or "comma separated" list.

Although commas are the most common delimiter for lists, any character (or set of characters) can be used. For example, ColdFusion Lists:ColdFusion Arrays:ColdFusion Structures is no less a list than the comma delimited list.

Creating Lists in ColdFusion

This is easy. You can create a list in ColdFusion just the same you would create any other variable. Just remember to throw in a delimiter:

Copy to Clipboard
<cfset coldfusion_list = "ColdFusion Lists,ColdFusion Arrays,ColdFusion Structures">

In reality, you would probably create the list dynamically by looping through a set of items. You will often need to convert an array into a list or a list to an array (see below).

ColdFusion List Functions

Once you have your list, you can use ColdFusion to work with it. ColdFusion provides many functions for working with lists. Some of the common things you might need to do are listed below.

Find out the list length:
Copy to Clipboard
Loop through the list:
Copy to Clipboard
<cfloop list="#coldfusion_list#" index="i">
   <cfoutput> #i# <br /></cfoutput>
Add items to the list:
  • Add an item to the end of the list. The following adds "ColdFusion Functions" to the end of the list:

    Copy to Clipboard
    listAppend(coldfusion_list, "ColdFusion Functions")
  • Add an item to the beginning of the list. The following adds "ColdFusion Functions" to the beginning of the list:

    Copy to Clipboard
    listPrepend(coldfusion_list, "ColdFusion Functions")
  • Add an item somewhere in the middle of the list. The following adds "ColdFusion Functions" to position 2 of the list:

    Copy to Clipboard
    listInsertAt(coldfusion_list, "2", "ColdFusion Functions")
  • The above functions also accept an optional "delimiter" field, which, if needed, you place at the end. For example: listAppend(coldfusion_list, "ColdFusion Functions", ":")

Find out if the list contains a given string:
Copy to Clipboard
listContains(coldfusion_list, "ColdFusion")

...or if you don't want it to be case sensitive:

Copy to Clipboard
listContainsNoCase(coldfusion_list, "ColdFusion")
Convert a list into an array:
Copy to Clipboard
Convert an array into a list:
Copy to Clipboard

Enjoy this page?