O'Reilly logo

Programming ColdFusion MX, 2nd Edition by Rob Brooks-Bilson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required