Basic array techniques
109
For more information about these array functions, see
CFML Reference
.
Deleting elements from an array
Use the
ArrayDeleteAt
function to delete data from the array at a particular index, instead of
setting the data value to zero or an empty string. If you remove data from an array, the array
resizes dynamically, as the following example shows:
<!--- Create an array with three elements --->
<cfset firstname=ArrayNew(1)>
<cfset firstname[1]="Robert">
<cfset firstname[2]="Wanda">
<cfset firstname[3]="Jane">
<!--- Delete the second element from the array --->
<cfset temp=ArrayDeleteAt(firstname, 2)>
<!--- Display the array length (2) and its two entries,
which are now "Robert" and "Jane" --->
<cfoutput>
The array now has #ArrayLen(firstname)# indexes<br>
The first entry is #firstname[1]#<br>
The second entry is #firstname[2]#<br>
</cfoutput>
The
ArrayDeleteAt
function removed the original second element and resized the array so that
it has two entries, with the second element now being the original third element.
Copying arrays
You can copy arrays of simple variables (numbers, strings, Boolean values, and date-time values)
by assigning the original array to a new variable name. You do not have to use
ArrayNew
to create
the new array first. When you assign the 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 and
populates a two-element array. It then copies the original array, changes one element of the copied
array and dumps both arrays. As you can see, the original array is unchanged and the copy has a
new second element.
<cfset myArray=ArrayNew(1)>
<cfset myArray[1]="First Array Element">
<cfset myArray[2]="Second Array Element">
<cfset newArray=myArray>
<cfset newArray[2]="New Array Element 2">
<cfdump var=#myArray#><br>
<cfdump var=#newArray#>
If your array contains complex variables (structures, query objects, or external objects such as
COM objects) assigning the original array to a new variable does not make a complete copy of the
original array. The array structure is copied; however, the new array does not get its own copy of
the complex data, only references to it. To demonstrate this behavior, run the following code:
Create an array that contains a structure.<br>
<cfset myStruct=StructNew()>
<cfset myStruct.key1="Structure key 1">
<cfset myStruct.key2="Structure key 2">
<cfset myArray=ArrayNew(1)>
<cfset myArray[1]=myStruct>
<cfset myArray[2]="Second array element">
<cfdump var=#myArray#><br>
<br>
Copy the array and dump it.<br>
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: ......