|
ColdFusion Day 41: ================ WORKING WITH COLDFUSION DATA STRUCTURES * Understanding and Using
Lists: More than just strings.
* Understanding and Using Arrays.
* Understanding and Using Structures and Associated Arrays. 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 data type including numbers,
strings, Boolean values, and lists. Structures
are new 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
STRINGS 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 switch-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:
"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
"1;2;3;4;5;6;7;8;9;0"
"ab|c|defg|h|i|jkl|mno|pqrstuvwxy|z" 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:
"1,2|3,4" 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 "2|3" and
"4") with a comma as the delimiter.
* A four-element list ("1" and "2" and
"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,|cd" if both commas and vertical bars are
valid delimiter characters, then the list has two elements: "ab" and
"cd". NOTE When working with lists, you can generally assume that the
delimiter is a comma unless otherwise specified. In most of the functions and
tags that work with lists, a comma is treated as the default delimiter unless
others are specified. ColdFusion
provides several functions that you can use to create and manipulate the
content of lists. You will take a close look at the following functions in this
section:
* ListLen * ListFirst * ListLast * ListRest * ListAppend * ListPrepend * ListChangeDelims * ListGetAt * ListInsertAt * ListSetAt * ListDeleteAt * ListFind * ListFindNoCase * ListContains * ListContainsNoCase ListLen 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 ListLen function
returns the number of elements in a list passed as an argument: ListLen(List,Delimiters).
If no delimiters are specified, the default comma is assumed to be the
delimiter. For
instance, the following code:
<CFOUTPUT>
#ListLen("1,2|3,4","1")#<BR>
#ListLen("1,2|3,4)#<BR>
#ListLen("1,2|3,4",",|")#
</CFOUTPUT> produces
the following output:
2
3
4 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, no delimiter is 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: ListFirst(List,Delimiters). As an example, the following code:
<CFOUTPUT>
#ListFirst("1,2|3,4","1")#<BR>
#ListFirst("1,2|3,4)#<BR>
#ListFirst("1,2|3,4",",|")#
</CFOUTPUT> produces
the following output:
1,2
1
1 ListLast As
expected, ListLast performs the opposite job of ListFirst: It returns the last
element in a list. ListLast takes the form ListLast(List,Delimiters). ListRest As
a corollary to ListFirst, ListRest returns a given list except for the first
element. Used together, ListFirst and ListRest return all elements in a list.
As would be expected, ListRest takes the form ListRest(List,Delimiters). Listing
13.1 shows the complementary roles of ListFirst and ListRest. Listing
13.1: listrest.cfm
<CFSET TheList = "1,2|3,4">
<TABLE BORDER=1>
<TR>
<TD>DELIMITER</TD>
<TD>FIRST</TD>
<TD>REST</TD>
</TR>
<TR>
<CFOUTPUT>
<TD>|</TD>
<TD>#ListFirst("1,2|3,4","1")#</TD>
<TD>#ListRest("1,2|3,4","1")#</TD>
</CFOUTPUT>
</TR>
<TR>
<CFOUTPUT>
<TD>,</TD>
<TD>#ListFirst("1,2|3,4","1")#</TD>
<TD>#ListRest("1,2|3,4","1")#</TD>
</CFOUTPUT>
</TR>
<TR>
<CFOUTPUT>
<TD>,|</TD>
<TD>#ListFirst("1,2|3,4","1")#</TD>
<TD>#ListRest("1,2|3,4","1")#</TD>
</CFOUTPUT>
</TR>
</TABLE> This code
produces results like those in figure 13.1 which can be viewed online at http://www.accc.net.au/sybex/listrest.cfm Figure 13.1: Using ListRest
DELIMITER
FIRST REST |
1,2 3,4
,
1 2|3,4
,| 1 2|3,4 |