User Guide

Creating and Using Structures 127
Creating and Using Structures
This section explains how to use the structure functions to create and use structures
in ColdFusion. The sample code in this section uses a structure called employee,
which is used to add new employees to a corporate information system.
Creating structures
You create structures by assigning a variable name to the structure with the
StructNew function:
<cfset mystructure=StructNew()>
For example, to create a structure named employee, use this syntax:
<cfset employee=StructNew()>
Now the structure exists and you can add data to it.
Adding data elements to structures
After you create a structure, you add key-value pairs to the structure using the
StructInsert function:
<cfset value=StructInsert(structure_name, key, value
[, AllowOverwrite])>
The AllowOverwrite parameter is optional and can be either True or False. You can
use it to specify whether an existing key should be overwritten. The default is False.
When adding string values to a structure, enclose the string in quotation marks. For
example, to add a key, John, with a value, Sales, to an existing structure called
Departments, use this syntax:
<cfset value=StructInsert(Departments, "John", "Sales")>
The following example shows how to add content to a sample structure named
employee, building the content of the value fields dynamically using form variables:
<cfset rc=StructInsert(employee, "firstname", "#FORM.firstname#")>
<cfset rc=StructInsert(employee, "lastname", "#FORM.lastname#")>
<cfset rc=StructInsert(employee, "email", "#FORM.email#")>
<cfset rc=StructInsert(employee, "phone", "#FORM.phone#")>
<cfset rc=StructInsert(employee, "department", "#FORM.department#")>