Variables in CFML; Mixing Notation

There are many cases where you must mix notations in the same reference to retrieve the value that you need. The most common of which is accessing query values using Dot notation to specify the column and Indexed notation to specify the row in the form queryName.ColumnName[RowNumber]. This also will occur when you scope references to arrays as in Request.myArray[1].

However as your applications become more complicated you will often find the need (or desire) to create complex, nested data objects. Structures, arrays and queries can hold structures, arrays and queries to an effectively infinite level.

Careful planning should be made to properly consider how you will construct these complex nests of data and special thought should be given to how you will access the data within them. Consider the following complex data set:

<cfset MainStruct = StructNew()>
<cfset MainStruct.ArrayOne = ArrayNew[1]>
<cfset MainStruct.ArrayTwo = ArrayNew[3]>
<cfset MainStruct.SubStruct = StructNew()>
<cfset MainStruct.SubStruct.ArrayOne = ArrayNew[1]>
<cfset MainStruct.SubStruct.ArrayTwo = ArrayNew[3]>
<cfset MainStruct.QueryOne = QueryNew("ColOne, ColTwo")>

Assuming that the above were fully populated with data all of the following (and many others) would be legal references to values within the structure and its children:

  • MainStruct
  • MainStruct.ArrayOne[1]
  • MainStruct.ArrayTwo[2][5][1]
  • MainStruct["SubStruct"]["ArrayOne"][34]
  • MainStruct["SubStruct"].ArrayTwo[2][3][4]
  • MainStruct.QueryOne.ColOne[2]
  • MainStruct["QueryOne"].ColOne[2]
  • MainStruct["QueryOne"]["ColOne"][2]

Although such complex creations can be incredibly useful organizers of data it should be clear that they can quickly grow out of control. Creating, and maintaining, standards for both the creation and referencing of such objects is highly recommended.

17 Current Sessions; Time: 02:34:37 07-01-2009; Tick: 1297