View comments | RSS feed

Referencing complex structures

When a structure contains another structure, you reference the data in the nested structure by extending either object.property or associative array notation. You can even use a mixture of both notations.

For example, if structure1 has a key key1 whose value is a structure that has keys struct2key1, struct2key2, and so on, you can use any of the following references to access the data in the first key of the embedded structure:

Structure1.key1.Struct2key1
Structure1["key1"].Struct2key1
Structure1.key1["Struct2key1"]
Structure1["key1"]["Struct2key1"]

The following example shows various ways you can reference the contents of a complex structure:

<cfset myArray=ArrayNew(1)>
<cfset myArray[1]="2">
<cfset myArray[2]="3">
<cfset myStruct2=StructNew()>
<cfset myStruct2.struct2key1="4">
<cfset myStruct2.struct2key2="5">
<cfset myStruct=StructNew()>
<cfset myStruct.key1="1">
<cfset myStruct.key2=myArray>
<cfset myStruct.key3=myStruct2>
<cfdump var=#myStruct#><br>

<cfset key1Var="key1">
<cfset key2Var="key2">
<cfset key3Var="key3">
<cfset var2="2">

<cfoutput>
Value of the first key<br>
#mystruct.key1#<br>
#mystruct["key1"]#<br>
#mystruct[key1Var]#<br>
<br>
Value of the second entry in the key2 array<br>
#myStruct.key2[2]#<br>
#myStruct["key2"][2]#<br>
#myStruct[key2Var][2]#<br>
#myStruct[key2Var][var2]#<br>
<br>
Value of the struct2key2 entry in the key3 structure<br>
#myStruct.key3.struct2key2#<br>
#myStruct["key3"]["struct2key2"]#<br>
#myStruct[key3Var]["struct2key2"]#<br>
#myStruct.key3["struct2key2"]#<br>
#myStruct["key3"].struct2key2#<br>
<br>
</cfoutput>

Reviewing the code

The following table describes the code:

Code Description
<cfset myArray=ArrayNew(1)>
<cfset myArray[1]="2">
<cfset myArray[2]="3">
<cfset myStruct2=StructNew()>
<cfset myStruct2.struct2key1="4">
<cfset myStruct2.struct2key2="5">
<cfset myStruct=StructNew()>
<cfset myStruct.key1="1">
<cfset myStruct.key2=myArray>
<cfset myStruct.key3=myStruct2>

Create a structure with three entries: a string, an array, and an embedded structure.

<cfdump var=#myStruct#><br>

Display the complete structure.

<cfset key1Var="key1">
<cfset key2Var="key2">
<cfset key3Var="key3">
<cfset var2="2">

Create variables containing the names of the myStruct keys and the number 2.

<cfoutput>
Value of the first key<br>
#mystruct.key1#<br>
#mystruct["key1"]#<br>
#mystruct[key1Var]#<br>
<br>

Output the value of the structure's key1 (string) entry using the following notation:

  • object.property notation
  • associative array notation with a constant
  • associative array notation with a variable
Value of the second entry in the
key2 array<br> #myStruct.key2[2]#<br> #myStruct["key2"][2]#<br> #myStruct[key2Var][2]#<br> #myStruct[key2Var][var2]#<br> <br>

Output the value of the second entry in the structure's key2 array using the following notation:

  • object.property notation
  • associative array notation with a constant
  • associative array notation with a variable
  • associative array notation with variables for both the array and the array index
Value of the struct2key2 entry in
the key3 structure<br> #myStruct.key3.struct2key2#<br> #myStruct["key3"]["struct2key2"]#<br> #myStruct[key3Var]["struct2key2"]#<br> #myStruct.key3["struct2key2"]#<br> #myStruct["key3"].struct2key2#<br> <br> </cfoutput>

Output the value of second entry in the structure's key3 embedded structure using the following notation:

  • object.property notation
  • associative array notation with two constants
  • associative array notation with a variable and a constant
  • object.property notation followed by associative array notation
  • associative array notation followed by object.property notation

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | KnowledgeBase | Bug Reporting

Version 7

Comments


Ashish-Saxena said on Jul 20, 2007 at 3:22 AM :
We know “A structure's key must be a string.”, but structure's key name (not key value) may be a numeric also for example if array index (with in a structure) is using as structure key, following scenior may be possible:

<cfset myArray=ArrayNew(1)>
<cfset myArray[1]="2">
<cfset myArray[2]="3">
<cfset myStruct2=StructNew()>
<cfset myStruct2.struct2key1="4">
<cfset myStruct2.struct2key2="5">
<cfset myStruct2.22="Value22">
<cfset myStruct=StructNew()>
<cfset myStruct.key1="1">
<cfset myStruct.key2=myArray>
<cfset myStruct.key3=myStruct2>

Note the line of code <cfset myStruct2.22="Value22">

<cfloop collection="#myStruct2#" item="key">
<cfoutput>
Key: "#key#" is
<cfif IsNumeric(key)>
"a Numeric"
<cfelse>
"Not a Numeric"
</cfif><br />
</cfoutput>
</cfloop>

Output:
Key: "STRUCT2KEY1" is "Not a Numeric"
Key: "22" is "a Numeric"
Key: "STRUCT2KEY2" is "Not a Numeric"

There may be following two cases:
If structure key name(not its value) is of type numeric then remember following points:
(1)With in square bracket[] :
(i) Numaric key name can be either enclosed in quote(single,double) or without quote,but other keys must be in quote otherwise gives Error.
(ii)With in square bracket, other key levels may be in array or object property notation.
myStruct.key3[22] = Value22
myStruct.key3["22"] = Value22
myStruct.key3['22'] = Value22
myStruct["key3"]["22"] = Value22
ERROR :(If Key3 is not in quote.) Variable KEY3 is undefined. myStruct[key3]["22"]
(2)Not in square bracket[] :
(1)Only the dot(object property) notation (in all levels) is used.
ERROR- myStruct["key3"].22
myStruct.key3.22 = Value22
(3)Use built in function StructFind().
StructFind(myStruct.key3,"22") = Value22

If array index is using as structure key(i.e. numeric key, not value of key):
(1)With in square bracket[] :
(i) Numaric(Indexed) key name can be either enclosed in quote(single,double) or without quote,but other keys must be in quote otherwise gives Error.
(ii)With in square bracket, other key levels may be in array or object property notation.
myStruct.key2[2] = 3
myStruct.key2["2"] = 3
myStruct.key2['2'] = 3
myStruct["key2"]['2'] = 3
ERROR :(If key2 is not in quote.) Variable KEY2 is undefined.myStruct[key2]['2']
(2)Not in square bracket[] :
Neither array nor dot notation can be used.
ERROR :myStruct.key2.2
ERROR :myStruct["key2"].2
(3)Cannot use built in function StructFind().
Both lines give error
You have attempted to dereference a scalar variable of type class coldfusion.runtime.Array as a structure with members.
ERROR :StructFind(myStruct.key2,2)
ERROR :StructFind(myStruct.key2,"2")
Ashish-Saxena said on Jul 20, 2007 at 3:57 AM :
Key names can be derived by combinations of variables:
I am adding a line of code in above code of structure
<cfset myStruct.type="key">
See the last two lines of code and its output:

myStruct.type = key
mystruct.key1 = 1
myStruct.key2[2] = 3
myStruct["key3"]["struct2key2"] = 5
myStruct.key2[mystruct.key1 + mystruct.key1] = 3
myStruct["key"& myStruct.key2[2]]["struct2key2"] = 5
myStruct[ myStruct.type & myStruct.key2[2]]["struct2key2"] = 5
halL said on Jul 20, 2007 at 8:04 AM :
Here is full working code based on Ahsish-Saxena's sample.

<cfscript>
myStruct=StructNew();
myStruct.type = "key";
mystruct.key1 = 1;
myStruct.key2[1] = 3;
myStruct["key3"]["struct2key2"] = 5;
myStruct.key2[mystruct.key1 + mystruct.key1] = 9;
myStruct["key"& myStruct.key2[2]]["struct2key2"] = 5;
myStruct[ myStruct.type & myStruct.key2[2]]["struct2key2"] = 5;
</cfscript>

<cfdump var=#myStruct#><br>
halL said on Jul 20, 2007 at 8:32 AM :
This is not an official Adobe response, but my personal observation:
I'd be careful about using all-numeric structure key names.
As a general rule, ColdFusion correctly converts between numeric values and their string representations.
Therefore, you can use an-all numeric structure key because it is really a string of numeric characters.
However, doing so results in the "interesting" behaviors that Ashish-Saxena points out.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/7/htmldocs/00000960.htm