Copying structures
ColdFusion provides several ways to copy structures and create structure references. The
following table lists these methods and describes their uses:
Technique
Duplicate
function
StructCopy
function
Variable
assignment
The following example shows the different effects of copying, duplicating, and assigning structure
variables:
Create a new structure<br>
<cfset myNewStructure=StructNew()>
<cfset myNewStructure.key1="1">
<cfset myNewStructure.key2="2">
<cfset myArray=ArrayNew(1)>
<cfset myArray[1]="3">
<cfset myArray[2]="4">
<cfset myNewStructure.key3=myArray>
<cfset myNewStructure2=StructNew()>
<cfset myNewStructure2.Struct2key1="5">
<cfset myNewStructure2.Struct2key2="6">
<cfset myNewStructure.key4=myNewStructure2>
<cfdump var=#myNewStructure#><br>
<br>
A StructCopy copied structure<br>
<cfset CopiedStruct=StructCopy(myNewStructure)>
<cfdump var=#CopiedStruct#><br>
<br>
A Duplicated structure<br>
<cfset dupStruct=Duplicate(myNewStructure)>
<cfdump var=#dupStruct#><br>
<br>
A new reference to a structure<br>
<cfset structRef=myNewStructure>
Use
Makes a complete copy of the structure. All data is copied from the original
structure to the new structure, including the contents of structures, queries, and
other objects. As a result changes to one copy of the structure have no effect on
the other structure.
This function is useful when you want to move a structure completely into a new
scope. In particular, if a structure is created in a scope that requires locking (for
example, Application), you can duplicate it into a scope that does not require
locking (for example, Request), and then delete it in the scope that requires
locking
Makes a shallow copy of a structure. It creates a new structure and copies all
simple variable and array values at the top level of the original structure to the
new structure. However, it does not make copies of any structures, queries, or
other objects that the original structure contains, or of any data inside these
objects. Instead, it creates a reference in the new structure to the objects in the
original structure. As a result, any change to these objects in one structure also
changes the corresponding objects in the copied structure.
The Duplicate replaces this function for most, if not all, purposes.
Creates an additional reference, or alias, to the structure. Any change to the data
using one variable name changes the structure that you access using the other
variable name.
This technique is useful when you want to add a local variable to another scope
or otherwise change a variable's scope without deleting the variable from the
original scope.
Creating and using structures
119
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