Copy link to clipboard
Copied
Hello all,
I have an example that is causing me trouble.
If I do the following, things work as expected.
CODE:
<cfoutput query="Customers">
<cfset var2 = listAppend(var2, #Customers.accno#)>
</cfoutput>
OUTPUT: #var2#
RESULTS: 553,610
The problem is that my output changes unexpectedly if I change my code.
CODE:
<cfoutput query="Customers">
<cfset var#Customers.key# = listAppend(var#Customers.key#, #Customers.accno#)>
</cfoutput>
OUTPUT: var#Customers.key#
RESULTS: var2,610
For a reason I haven't been able to determine, the ListAppend (or simple concatenate for that matter) is pre-pending the name of the dynamic variable to the list. I can only assume it is the manner in which ColdFusion evaluates the statement but I'm not sure how to work around it.
Thanks for any help!
Copy link to clipboard
Copied
CODE:
<cfoutput query="Customers">
<cfset variables["var" & Customers.key] = listAppend(variables["var" & Customers.key], variables.Customers.accno)>
</cfoutput>
Array notation is a much more reliable way to work with dynamic variable naming.
Copy link to clipboard
Copied
I don't believe I've seen the variables[] array. Is that a built-in construct?
I'm assuming there's some magic there since without it the code functions just as in my original post.
Thanks
Copy link to clipboard
Copied
mmcarthey wrote:
I don't believe I've seen the variables[] array. Is that a built-in construct?
I'm assuming there's some magic there since without it the code functions just as in my original post.
Thanks
Yes, starting with CF 6, when ColdFusion was ported to a Java base, all the scopes of ColdFusion are structure arrays, including the variables[] scope which is where unscoped variables live.
Copy link to clipboard
Copied
<cfoutput query="Customers"> <cfset var#Customers.key# = listAppend(var#Customers.key#, #Customers.accno#)>
</cfoutput>
Which version of CF were you able to get that code to run?
The <cfset> line has two syntax errors in it: both reverences to the dynamic variable you're trying to create are invalid, and will not compile on any version of CF I know of.
??
--
Adam