Copying structures

ColdFusion provides several ways to copy structures and create structure references. The following table lists these methods and describes their uses:

Technique

Use

Duplicate function

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.

StructCopy function

Makes a shallow copy of a structure. It creates a 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 function replaces this function for most, if not all, purposes.

Variable assignment

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 the scope of a variable without deleting the variable from the original scope.

The following example shows the different effects of copying, duplicating, and assigning structure variables:

Create a 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> 
<cfdump var=#structRef#><br> 
 
<br> 
Change a string, array element, and structure value in the StructCopy copy.<br> 
<br> 
<cfset CopiedStruct.key1="1A"> 
<cfset CopiedStruct.key3[2]="4A"> 
<cfset CopiedStruct.key4.Struct2key2="6A"> 
Original structure<br> 
<cfdump var=#myNewStructure#><br> 
Copied structure<br> 
<cfdump var=#CopiedStruct#><br> 
Duplicated structure<br> 
<cfdump var=#DupStruct#><br> 
Structure reference 
<cfdump var=#structRef#><br> 
<br> 
Change a string, array element, and structure value in the Duplicate.<br> 
<br> 
<cfset DupStruct.key1="1B"> 
<cfset DupStruct.key3[2]="4B"> 
<cfset DupStruct.key4.Struct2key2="6B"> 
Original structure<br> 
<cfdump var=#myNewStructure#><br> 
Copied structure<br> 
<cfdump var=#CopiedStruct#><br> 
Duplicated structure<br> 
<cfdump var=#DupStruct#><br> 
Structure reference 
<cfdump var=#structRef#><br> 
<br> 
Change a string, array element, and structure value in the reference.<br> 
<br> 
<cfset structRef.key1="1C"> 
<cfset structRef.key3[2]="4C"> 
<cfset structRef.key4.Struct2key2="6C"> 
Original structure<br> 
<cfdump var=#myNewStructure#><br> 
Copied structure<br> 
<cfdump var=#CopiedStruct#><br> 
Duplicated structure<br> 
<cfdump var=#DupStruct#><br> 
Structure reference 
<cfdump var=#structRef#><br> 
<br> 
Clear the original structure<br> 
<cfset foo=structclear(myNewStructure)> 
Original structure:<br> 
<cfdump var=#myNewStructure#><br> 
Copied structure<br> 
<cfdump var=#CopiedStruct#><br> 
Duplicated structure<br> 
<cfdump var=#DupStruct#><br> 
Structure reference:<br> 
<cfdump var=#structRef#><br>