94
Chapter 4: Using Expressions and Pound Signs
Dynamic variable limitation
When you use a dynamic variable name in quotes 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 quotes
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
Chapter 5, “Structure notation,” on page 114
.) 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 99
.
Using dynamic evaluation
The following sections describe how to use dynamic evaluation and create dynamic expressions.
ColdFusion dynamic evaluation functions
The following table describes the functions that perform dynamic evaluation and are useful in
evaluating dynamic expressions:
Function
Purpose
DE
Escapes any double quotes in the argument and wraps the result in double
quotes. The
DE
function is particularly useful with the
IIF
function, to prevent the
function from evaluating a string to be output.
For an example of using the
DE
function with the
IIF
function, see
“Using the IIF
function” on page 98
.
Evaluate
Takes one or more string expressions and dynamically evaluates their contents
as expressions from left to right. (The results of an evaluation to the left can have
meaning in an expression to the right.) Returns the result of evaluating the
rightmost argument.
For more information on this function see
“About the Evaluate function”
on page 95
.
Summary of Contents for ColdFusion MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......