Adobe COLDFUSION 9 Manual page 102

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
The CFML Programming Language
You can also create a structure by assigning data to a variable. For example, each of the following lines creates a
structure named
with one element,
myStruct
<cfset coInfo.name = "Adobe Systems Incorporated">
<cfset coInfo["name"] = "Adobe Systems Incorporated">
<cfset coInfo = {name = "Adobe Systems Incorporated"}>
When you use structure notation to create a structure, as shown in the third example, you can populate multiple
structure fields. The following example shows this use:
<cfset coInfo={name="Adobe Systems Incorporated", industry="software"}
ColdFusion does not allow nested implicit creation of structures, arrays, or structures and arrays. The following line,
for example, generates an error:
<cfset myStruct = {structKey1 = {innerStructKey1 = "innerStructValue1"}}>
Similarly, you cannot use object.property notation on the left side of assignments inside structure notation. The
following statement, for example, causes an error:
<cfset myStruct={structKey1.innerStructKey1 = "innerStructValue1"}>
Instead of using these formats, use multiple statements, such as the following:
<cfset innerStruct1 = {innerStructKey1 = "innerStructValue1"}
<cfset myStruct1={structKey1 = innerStruct1}>
You cannot use a dynamic variable when you create a structure implicitly. For example, the following expression
generates an error:
<cfset i="coInfo">
<cfset "#i#"={name = ""Adobe Systems Incorporated"}>
Using implicitly created structures in functions and tags
You can use implicitly created structures directly in functions (including user-defined functions) and tags. For
example, the following code dumps an implicitly created structure.
<cfdump var="#{Name ="28 Weeks Later", Time = "7:45 PM"}#">
You can use array notation inside the structure notation, as shown in the following example:
<cfset student = {firstName="Jane", lastName="Janes", grades=[91, 78, 87]}>
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 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:
, that has the value Adobe Systems Incorporated.
name
and object.property notation to create a structure element called departments.John,
Last updated 8/5/2010
97

Advertisement

Table of Contents
loading

Table of Contents