Lists

Contact Us or call 1-877-932-8228
Lists

Lists

Lists are really nothing more than strings delimited by a specified character or set of characters. By default, the delimiter is a comma. A list can be created using <cfset>.

<cfset numlist="1,2,3,4,5">

The delimiter can be changed to any character or group of characters

<cfset beatles="Paul John George Ringo">

To following code shows how to loop through these lists.

Code Sample:

ListsAndArrays/Demos/ListLoops.cfm
<html>
<head>
<title>List Loops</title>
</head>

<body>
<h1>List Loops</h1>

<h2>List Loop 1</h2>
<cfset numlist="1,2,3,4,5">
<cfloop index="num" list="#numlist#">
	<cfoutput>#num#</cfoutput>
</cfloop>

<h2>List Loop 2</h2>
<cfset beatles="paul john ringo george">
<ul>
<cfloop index="beatle" list="#beatles#" delimiters=" ">
	<li><cfoutput>#beatle#</cfoutput></li>
</cfloop>
</ul>

</body>
</html>

List Functions

ColdFusion provides many built-in functions for working with lists. The table below shows some of the most common list functions.

List Functions
Function Description
ListLen() Determines the number of elements in a list.
ListFind() Returns the index of the first list element in which a specified value occurs. Case-sensitive.
ListFindNoCase() Determines the index of the first list element in which a specified value occurs. Case-insensitive.
ListContains() Returns the index of the first list element that contains a specified substring. Case-sensitive.
ListContainsNoCase() Returns the index of the first list element that contains a specified substring. Case-insensitive.
ListValueCount() Counts instances of a specified value in a list. The search is case-sensitive.
ListValueCountNoCase() Counts instances of a specified value in a list. The search is case-insensitive.
ListDeleteAt() Returns a copy of the list without the specified element.
ListGetAt() Gets a list element at a specified position.
ListSetAt() Replaces the contents of a list element.
ListSort() Sorts list elements according to a sort type and sort order.
ListAppend() Appends a list or element to a list.
ListPrepend() Inserts an element at the beginning of a list.
ListInsertAt() Inserts an element in a list.
ListFirst() Gets the first element of a list.
ListRest() Gets a list, without its first element.
ListLast() Gets the last element of a list.
ArrayToList() Converts a one-dimensional array to a list.
ListToArray() Converts a list to a one-dimensional array.
ReplaceList() Replaces occurrences of the elements from a delimited list in a string with corresponding elements from another delimited list. The search is case-sensitive.
ListChangeDelims() Changes a list delimiter.
ListQualify() Inserts a string at the beginning and end of list elements.
Next