106
Chapter 5: Using Arrays and Structures
Basic array techniques
The following sections describe how to reference array elements, create arrays, add and remove
array elements, and copy arrays.
Referencing array elements
You reference array elements by enclosing the index with brackets:
arrayName
[
x
] where
x
is the
index that you want to reference. In ColdFusion, array indexes are counted starting with position
1, which means that position 1 in the firstname array is referenced as firstname[1]. For 2D arrays,
you reference an index by specifying two coordinates:
myarray[1][1]
.
You can use ColdFusion variables and expressions inside the square brackets to reference an index,
as the following example shows:
<cfset myArray=ArrayNew(1)>
<cfset myArray[1]="First Array Element">
<cfset myArray[1 + 1]="Second Array" & "Element">
<cfset arrayIndex=3>
<cfset arrayElement="Third Array Element">
<cfset myArray[arrayIndex]=arrayElement>
<cfset myArray[arra 1]="Fourth Array Element">
<cfdump var=#myArray#>
Note:
The
IsDefined
function does not test the existence of array elements. Instead, put any code
that might try to access an undefined array element in a try block and use a catch block to handle
exceptions that arise if elements do not exist.
Creating arrays
In ColdFusion, you declare an array by assigning a variable name to the new array and specifying
its dimensions, as follows:
<cfset mynewarray=ArrayNew(
x
)>
where
x
is the number of dimensions (from 1 to 3) in the array that you want to create.
Once you declare an array, you can add array elements, which you can then reference using the
elements’ indexes.
For example, suppose you declare a 1D array called "firstname":
<cfset firstname=ArrayNew(1)>
The array firstname holds no data and is of an unspecified length. Next you add data to the array:
<cfset firstname[1]="Coleman">
<cfset firstname[2]="Charlie">
<cfset firstname[3]="Dexter">
After you add these names to the array, it has a length of 3.
Creating complex multidimensional arrays
ColdFusion supports dynamic multidimensional arrays. When you declare an array with the
ArrayNew
function, you specify the number of dimensions. You can create an asymmetrical array
or increase an existing array’s dimensions by nesting arrays as array elements.
It is important to know that when you assign one array (array1) to an element of another array
(array2), array1 is copied into array2. The original copy of array1 still exists, independent of
array2. You can then change the contents of the two arrays independently.
Summary of Contents for COLDFUSION MX 61-DEVELOPING COLDFUSION MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......