Adding and updating structure elements

You add or update a structure element to a structure by assigning the element a value or by using a ColdFusion function. It is simpler and more efficient to use direct assignment.

You can add structure key-value pairs by defining the value of the structure key, as the following example shows:

<cfset myNewStructure.key1="A new structure with a new key"> 
<cfdump var=#myNewStructure#> 
<cfset myNewStructure.key2="Now I've added a second key"> 
<cfdump var=#myNewStructure#>

The following code uses cfset and object.property notation to create a structure element called departments.John, and changes John’s department from Sales to Marketing. It then uses associative array notation to change his department to Facilities. Each time the department changes, it displays the results:

<cfset departments=structnew()> 
<cfset 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>