Working with cold Fusion Data Structure ColdFusion Help

So far in the book, you have worked with simple data types: numbers strings Boolean values and the like. ColdFusion however supports a collection of more advanced data types common to most programming languages. In ColdFusion these more sophisticated data types are lists arrays and structures. In all cases these data types combine collections of simpler data types into a more complex, structured relationship.

For instance a list is just that a list of strings in a predefined order. An array is an ordered collection of any data type including numbers strings Boolean values and lists. Structures were introduced in ColdFusion 4. They offer the capability to create sets of relationships by mapping one set of values to its counterpart in another set of values. Applying these data types opens the door to new possibilities in your ColdFusion templates.

Understanding And Using Lists More Than Just String

You have already had a brief introduction to lists. In Chapter 7 “Controlling the Flow of Your Templates” you encountered lists as the value of the VALUE attribute of the CFCASE tag in a witch-case construct

A list is simply a string value structured in a special way; it is a series of shorter strings separated by a delimiter. For instance the following are all lists:


In all cases the lists have a delimiter (the three used here are a comma, a semicolon and a vertical bar or pipe). These delimiters separate list elements that consist of one or more· characters other than the delimiter character.

Lists can also contain more than one delimiter character. For instance the following list can be interpreted in several ways:


There are three ways to break up this list depending on the selection of the delimiter character:

• A two-element list (“1,2” and “3,4”) with a vertical bar as the delimiter.
• A three-element list (“1” and “213” and “4”) with a comma as the delimiter.
• A four-element list (“1” and “2” and I 3″ and “4”) with both commas and vertical bars as delimiters

Another aspect of lists is that delimiters can be more than one character long. For instance, in the list ab Icd  if both commas and vertical bars are valid delimiter characters then the list has two elements: “ab” and “cd”.

Working with Lists

ColdFusion provides several functions that you can use to create and manipulate the contents of lists. You will take a close look at the following functions in this section:
• L+stt.en
• L; stF; rst
• ListLast
• L;stRest
• ListAppend
• ListPrepend
• ListChangeDelims
• Li stGetAt
• ListlnsertAt
• L;stSetAt
• ListDeleteAt
• ListFind
• L;stFindNoCase
• ListContains
• ListContainsNoCase


Because a list consists of a series of elements, when working with lists you need to be able to determine the number of elements in a list. The Li stLen function returns the number of elements in a list passed as an argument: Li stLen(L i stjDel imiters). If delimiters are not specified, the default comma is assumed to be the delimiter.

For instance, the following code

‘ListLen(‘l,213,4′,’, I’)’

produces the following output:


Notice the use of the second argument to specify delimiters in the first and last lines of the output. The last line specifies multiple delimiters (the comma and the vertical bar) by simply including them in the string that is the delimiter argument. In the second line of the output, a delimiter is not specified, which means that a comma is the delimiter.


ListFirst returns the first element in a list given an argument and an optional delimiter definition: ListFi rst(L i st[,Del tmt ters l ). As an example, the following code:

produces the following output:



As would be expected ListLast performs the opposite job of L; stFi rst: It returns the last element in a list. Li stLast takes the form L; stLast(L; st, Del ;m; ters).


As a corollary to L; stFfrst, L; stRest returns a given list except for the first element. Used together L; stFi rst and L; stRest return all elements in a list. As would be expected, L; stRest takes the form L; stRest(L; st [, Del imiters]).

Listing 14.1 shows the complementary roles of L; stFi rst arid li stRest.

<CFSET TheList – ‘1,213,.’>
<TO> 1</TO>
<TD>#ListFirstC’l,2I 3,4′)#</TO>
<TD>#ListRestC ‘1,2 I3,.’)f</TO>
<TO>, I</TO>
This code produces results like those in Figure 14.1



Posted on November 16, 2015 in Working with cold Fusion Data Structure

Share the Story

Back to Top
Share This