User Guide

Table Of Contents
90 Chapter 4: Using Expressions and Number Signs
The following example has the same result as the preceding example and is more efficient:
<cfoutput>
ProductName: #Form["product_" & i]#
</cfoutput>
In this code, ColdFusion does the following:
1.
Evaluates the expression in the associative array index brackets as the string "product_"
concatenated with the value of the variable i.
2.
Determines the value of the variable i; 1.
3.
Concatenates the string and the variable value to get product_1.
4.
Uses the result as the key value in the Form structure to get Form[product_1]. This associative
array reference accesses the same value as the object.attribute format reference Form.product_1;
in this case, Air popper.
This code format does not use any dynamic evaluation, but it achieves the same effect, of
dynamically creating a structure reference by using a string and a variable.
SetVariable function considerations
You can avoid using the
SetVariable function by using a format such as the following to set a
dynamically named variable. For example, the following lines are equivalent:
<cfset SetVariable("myVar" & i, myVal)>
<cfset "myVar#i#" = myVal>
In the second line, enclosing the myVar#i# variable name in quotation marks tells ColdFusion to
evaluate the name and process any text in number signs as a variable or function. ColdFusion
replaces the #i# with the value of the variable i, so that if the value of i is 12, this code is
equivalent to the line
<cfset myVar12 = myVal>
For more information on this usage, see “Using number signs to construct a variable name in
assignments” on page 85.
Using the IIF function
The
IIf function is a shorthand for the following code:
<cfif argument1>
<cfset result = Evaluate(argument1)>
<cfelse>
<cfset result = Evaluate(argument2)>
</cfif>
The function returns the value of the result variable. It is comparable to the use of the JavaScript
and Java ? : operator, and can result in more compact code. As a result, the
IIF function can be
convenient even if you are not using dynamic expressions.