Adobe COLDFUSION 9 Manual page 51

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
The CFML Programming Language
Arrays
Arrays are a way of storing multiple values in a table-like format that can have one or more dimensions. You create
arrays using a function or an assignment statement:
• The ColdFusion
ArrayNew
line creates an empty two-dimensional array:
<cfset myarray=ArrayNew(2)>
• A direct assignment creates an array and populates an array element. For example, the following line creates a two-
dimensional array and sets the value of row 1 column 2 to the current date.
<cfset myarray[1][2]=Now()>
You reference elements using numeric indexes, with one index for each dimension, as shown in the preceding
example.
You can create arrays with up to three dimensions directly. However, there is no limit on array size or maximum
dimension. To create arrays with more than three dimensions, create arrays of arrays.
After you create an array, you can use functions or direct references to manipulate its contents.
When you assign an existing array to a new variable, ColdFusion creates a new array and copies the old array's contents
to the new array. The following example creates a copy of the original array:
<cfset newArray=myArray>
For more information on using arrays, see
Structures
ColdFusion structures consist of key-value pairs, where the keys are text strings and the values can be any ColdFusion
data type, including other structures. Structures let you build a collection of related variables that are grouped under a
single name. To create a structure, use the ColdFusion
new, empty, structure called depts:
<cfset depts=StructNew()>
You can also create a structure by assigning a value in the structure. For example, the following line creates a new
structure called MyStruct with a key named MyValue, equal to 2:
<cfset MyStruct.MyValue=2>
Note: In ColdFusion versions through ColdFusion 7, this line created a Variables scope variable named
"MyStruct.MyValue" with the value 2.
After you create a structure, you can use functions or direct references to manipulate its contents, including adding
key-value pairs.
You can use either of the following methods to reference elements stored in a structure:
• StructureName
KeyName
.
• StructureName
KeyName
["
The following examples show these methods:
depts.John="Sales"
depts["John"]="Sales"
When you assign an existing structure to a new variable, ColdFusion does not create a new structure. Instead, the new
variable accesses the same data (location) in memory as the original structure variable. In other words, both variables
are references to the same object.
function creates an array and specifies its initial dimensions. For example, the following
"Using Arrays and
StructNew
"]
Last updated 8/5/2010
Structures" on page 82.
function. For example, the following line creates a
46

Advertisement

Table of Contents
loading

Table of Contents