User Guide

Table Of Contents
86 Chapter 4: Using Expressions and Number Signs
To construct a variable name this way, all the text on the left side of the equal sign must be in
quotation marks.
This usage is less efficient than using arrays. The following example has the same purpose as the
previous one, but requires less processing:
<cfset MyArray=ArrayNew(1)>
<cfset prodNo = 12>
<cfset myArray[prodNo] = "Widget">
Dynamic variable limitation
When you use a dynamic variable name in quotation marks on the left side of an assignment, the
name must be either a simple variable name or a complex name that uses object.property notation
(such as MyStruct.#KeyName#). You cannot use an array as part of a dynamic variable name. For
example, the following code generates an error:
<cfset MyArray=ArrayNew(1)>
<cfset productClassNo = 1>
<cfset productItemNo = 9>
<cfset "myArray[#productClassNo##productItemNo#]" = "Widget">
However, you can construct an array index value dynamically from variables without using
quotation marks on the left side of an assignment. For example, the preceding sample code works
if you replace the final line with the following line:
<cfset myArray[#productClassNo# & #productItemNo#] = "Widget">
Dynamically constructing structure references
The ability to use associative array notation to reference structures provides a way for you to use
variables to dynamically create structure references. (For a description of associative array
notation, see “Structure notation on page 109.) Associative array structure notation allows you
to use a ColdFusion expression inside the index brackets. For example, if you have a
productName structure with keys of the form product_1, product_2 and so on, you can use the
following code to display the value of productName.product_3:
<cfset prodNo = 3>
<cfoutput>
Product_3 Name: #ProductName["product_" & prodNo]#
<cfoutput>
For an example of using this format to manage a shopping cart, see “Example: a dynamic
shopping cart” on page 92.