Deleting Elements From An Array; Copying Arrays - MACROMEDIA COLDFUSION MX 61-DEVELOPING COLDFUSION MX Develop Manual

Developing coldfusion mx applications
Table of Contents

Advertisement

For more information about these array functions, see CFML Reference.

Deleting elements from an array

Use the
ArrayDeleteAt
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
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
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>
function to delete data from the array at a particular index, instead of
function removed the original second element and resized the array so that
to create
ArrayNew
Basic array techniques
109

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?

Questions and answers

This manual is also suitable for:

Coldfusion mx

Table of Contents