114
Creating and Using Structures
This section explains how to use the structure functions to create and use structures in
ColdFusion. The sample structure is called employee, and 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 to structures
After you've created 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. It can be
used to specify whether an existing key should be overwritten or not. 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")>
To change the value associated with a specific key, use the StructUpdate function. For
example, if John moves from the Sales department to the Marketing department, you
would use this syntax to update the Departments associative array:
<CFOUTPUT>
Personnel moves: #StructUpdate(Departments, "John", "Marketing")#
</CFOUTPUT>
Example of adding data to a structure
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:
Developing Web Applications with ColdFusion
Need help?
Do you have a question about the COLDFUSION 4.5-DEVELOPING WEB and is the answer not in the manual?