Notice: With the launch of Adobe Cookbooks, this site will no longer be accepting new entries or posting new content. Thanks to everyone who submitted content!

How do you copy a structure?

There are (at least) 3 ways to copy a structure. Assume an existing structure named myStruct.

1) Variable assignment: <cfset myNewStruct = myStruct />

This will create a shallow copy (or copy by reference) of myStruct. Any modifications made to myStruct will also affect myNewStruct.

2) structCopy(): <cfset myNewStruct = structCopy(myStruct) />

This built-in function will create a deep copy (or copy by value) of all top level keys and their values. This means any modifications to myStruct will NOT affect these values. However, any nested structs are shallow copies (by reference). This means that nested structures within myNewStruct will be affected by any change to nested structs within myStruct.

3) duplicate(): <cfset myNewStruct = duplicate(myStruct) />

This built-in function will create a deep copy (or copy by value) of the entire structure and any nested structures. There is no reference whatsoever to the original struct.

The bottom line: If you need a truly separate entity (a clone of the original struct), use duplicate().


This question was written by Charlie Griefer.
It was last updated on January 17, 2006 at 6:19:16 PM EST.

CFML Referenced

Duplicate()
StructCopy()

Categories

Data Structures

Comments

There are no comments for this entry.