ColdFusion 9.0 Resources |
Copying structuresColdFusion provides several ways to copy structures and create structure references. The following table lists these methods and describes their uses:
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> |