Lists

A list is a special type of string that contains delimited elements. For example, “a,b,c,d,e,f,g” is a list where “a”, “b”, “c”, “d”, “e”, “f”, and “g” are considered the list elements while the comma (,) is considered the delimiter. Lists are commonly used to store related items in a single variable. For example, you might use a list to store all the primary key values for a group of database records you want to delete.

It should be noted that ColdFusion treats list elements containing null values as though they don’t exist. For example, the list “1,2,,4” contains only three list elements (as far as ColdFusion is concerned) even though there is a null list element “,,”. The concept of a null list element shouldn’t be confused with a blank list element as in “1,2, ,4”, which contains four list elements, the space occupying list element three being counted. To further illustrate the point, consider Example 6-1, which creates three lists using cfset tags and then uses the ListLen( ) function to evaluate the number of elements in each list.

Example 6-1. Creating lists and evaluating their length

<cfset List1 = "1,2,3,4">
<cfset List2 = "1,2,,4">
<cfset List3 = "1,2, ,4">
   
<cfoutput>
List1 (#List1#) contains #ListLen(List1)# elements.<br>
List2 (#List2#) contains #ListLen(List2)# elements.<br>
List3 (#List3#) contains #ListLen(List3)# elements.
</cfoutput>

Executing the template in Example 6-1 results in the output shown in Figure 6-1.

Figure 6-1. Outputting the number of elements ...

Get Programming ColdFusion MX, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.