Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Using ListAppend and dynamic variables

Explorer ,
Jul 22, 2010 Jul 22, 2010

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!

757
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jul 22, 2010 Jul 22, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 22, 2010 Jul 22, 2010

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jul 22, 2010 Jul 22, 2010
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 22, 2010 Jul 22, 2010
<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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources