[thelist] CF query tip

Raymond Camden jedimaster at macromedia.com
Thu Jan 31 13:25:00 CST 2002


> structure and assigns it to a new variable.  StructCopy makes
> a copy of the
> *structure* of a structure and assigns it to a new variable.  This new
> variable will still reference the values in the structure it
> was copied
> from.  Duplicate makes a structure that is completely
> independent of the
> structure it was copied from.
>

Err - no. Unless I'm misreading you. StructCopy creates a true copy, not
a pointer, but only of simple values. If the structure has a key that is
a struct itself, that particular key in the new structore will be a
pointer.

Confusing? Considet the following:

<cfset simple = structNew()>
<cfset simple.apple = "ray">
<cfset copy = structCopy(simple)>

<cfoutput>
simple.apple is #simple.apple#<br>
copy.apple is #copy.apple#<br>
</cfoutput>

<cfset copy.apple = "camden">

<cfoutput>
<p>
After changing copy - vals are:<br>
simple.apple is #simple.apple#<br>
copy.apple is #copy.apple#<br>
</cfoutput>

<hr>

<cfset complex = structNew()>
<cfset complex.apple = "ray">
<cfset complex.test = structNew()>
<cfset complex.test.name = "Original">
<cfset newcopy = structCopy(complex)>

<cfoutput>
Current vals of complex and newcopy:
</cfoutput>
<cfdump var="#complex#">
<cfdump var="#newcopy#">

<cfset newcopy.apple = "camden">
<cfset newcopy.test.name = "Changed!">

<cfoutput>
<p>
After changing newcopy, here is complex and new copy again:
</cfoutput>
<cfdump var="#complex#">
<cfdump var="#newcopy#">

Ok, sorry for the big code dump. Our first example (all before the <hr>)
uses a simple struct. We can modify the copy with no problems. The
second examples shows a more complex structure. The key test, is a
struct itself. This time when we modify newcopy, it actually changes
complex as well.

Of course, the moral of the story is - if you aren't sure what is in
your struct, use duplicate. Or, heck, always use duplicate.

FYI, you should also use duplicate when copying queries as well. Also
watch out for arrays that have structs inside.

Also, if you are running 4.5.1, be sure to get the Duplicate() hot fix.

=======================================================================
Raymond Camden, Principal Spectra Compliance Engineer for Macromedia

Email    : jedimaster at macromedia.com
Yahoo IM : morpheus

"My ally is the Force, and a powerful ally it is." - Yoda




More information about the thelist mailing list