User Guide

128 Chapter 8 Handling Complex Data with Structures
Updating values in structures
You can update structure element values in a cfset tag or a SructUpdate function.
Updating a structure with cfset
You can use the cfset tag to update structure values (but not keys). For example, the
following code uses
cfset and Object.property notation to change Johns
department from Sales to Marketing. It then uses associative array notation to
change his department to Facilities. Each time the department changes, it outputs
the results:
<cfset departments=structnew()>
<cfset value=StructInsert(departments, "John", "Sales")>
<cfoutput>
Before the first change, John was in the #departments.John#
Department<br>
</cfoutput>
<cfset Departments.John = "Marketing">
<cfoutput>
After the first change, John is in the #departments.John#
Department<br>
</cfoutput>
<cfset Departments.John = "Facilities">
<cfoutput>
After the second change, John is in the #departments.John#
Department<br>
</cfoutput>
Updating a structure with StructUpdate
You can also use the StructUpdate function to change the value associated with a
specific key. Because
StructUpdate is a ColdFusion function, you must use it inside
a ColdFusion tag. In some cases, you can use the
cfoutput or cfset tag. You can also
use the
cfscript tag to tell ColdFusion to run a function. The following example
uses a
StructUpdate function in a cfscript tag to change a structure value. Note
that you must follow a statement in a
cfscript tag with a semicolon.
<cfset departments=structnew()>
<cfset value=StructInsert(departments, "John", "Sales")>
<cfoutput>
Before the change, John was in the #departments.John# Department<br>
</cfoutput>
<cfscript>StructUpdate(Departments, "John", "Marketing"); </cfscript>
<cfoutput>
After the change, John is in the #departments.John# Department<br>
</cfoutput>
For more information on using cfscript, see Chapter 13, Extending ColdFusion
Pages with CFML Scripting on page 243