ColdFusion 9.0 Resources |
Creating structuresIn ColdFusion, you can create structures explicitly by using a function, and then populate the structure using assignment statements or functions, or you can create the structure implicitly by using an assignment statement. Creating structures using functionsYou can create structures by assigning a variable name to the structure with the StructNew function as follows: <cfset structName = StructNew()> For example, to create a structure named departments, use the following syntax: <cfset departments = StructNew()> This statement creates an empty structure to which you can add data. Creating structures implicitlyYou can create an empty structure implicitly, as in the following example: <cfset myStruct = {}> You can also create a structure by assigning data to a variable. For example, each of the following lines creates a structure named myStruct with one element, name, that has the value Adobe Systems Incorporated. <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"}> |