Highlighted

ArrayAppend Bug

New Here ,
Jan 27, 2016

Copy link to clipboard

Copied

Hello Everyone,

I am the webmaster for a school district, and we have a system where parents enter the name of their street and the city they live in.

The system then returns which schools their children will go to based on their address.

We are running ColdFusion 11 and I recently installed Update 7.

After this update was installed this system stopped working properly.

Instead of returning unique entries, it returns the same entry multiple times.

I did some testing and found that when using ArrayAppend() to add a Structure to an array, that it overwrites all of the entries in the Array to match the entry it just added.

here is a section of my code

<cfset StreetArray = ArrayNew(1)>

<cfloop query="Streets">

    <cfset StreetRow['Street'] = STREET_NAME>

    <cfset StreetRow['Range'] = LOW_RANGE & ' - ' & HIGH_RANGE>

    <cfif EVEN_ODD is "E">

          <cfset StreetRow['EvenOdd'] = "Even">

    <cfelseif EVEN_ODD is "O">

          <cfset StreetRow['EvenOdd'] = "Odd">

    <cfelse>

          <cfset StreetRow['EvenOdd'] = "Both">

    </cfif>

    <cfset StreetRow['City'] = Streets.CITY>

    <cfset StreetRow['Zip'] = ZIP>

    <cfset StreetRow['Elementary'] = ELEMENTARY_SCHOOL>

    <cfset StreetRow['Middle'] = MIDDLE_SCHOOL>

    <cfset StreetRow['High'] = HIGH_SCHOOL>

    <cfset ArrayAppend(StreetArray, StreetRow)>

    <cfdump var="#StreetRow#">

    <cfdump var="#StreetArray#">

</cfloop>

Now i added the cfdumps on the end for testing.Here is what I get after the dumps.

bug.png

As you can see the StreetRow variable is updating properly, but after the second ArrayAppend() both entries in the array are the same even though the array was correct the first time.

I would like to also note that this does not happen if I explicitly create the structure inside the ArrayAppend() function so:

<cfset ArrayAppend(StreetArray, {Street=STREET_NAME,Range=LOW_RANGE & ' - ' & HIGH_RANGE, City=Streets.CITY,Zip=ZIP,Elementary=ELEMENTARY_SCHOOL,Middle=MIDDLE_SCHOOL,High=HIGH_SCHOOL,EvenOdd=EvenOdd})>

does not create the same issue.

This appears to be some kind of Bug, unless I am missing something...

It looks like ColdFusion is waiting until the loop is done before calling ArrayAppend() for all the loops. This works fine if you explicitly create the structures inside the function, but not so much if you are using a variable.

Complex datatypes, such as structures, are passed by reference.  In your loop you are repopulating the same structure with information.  When you append to the array, you are appending a reference to the structure. 

Add <cfset StreeRow = StructNew()> at the beginning of the loop.

Views

421

Likes

Translate

Translate

Report

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

ArrayAppend Bug

New Here ,
Jan 27, 2016

Copy link to clipboard

Copied

Hello Everyone,

I am the webmaster for a school district, and we have a system where parents enter the name of their street and the city they live in.

The system then returns which schools their children will go to based on their address.

We are running ColdFusion 11 and I recently installed Update 7.

After this update was installed this system stopped working properly.

Instead of returning unique entries, it returns the same entry multiple times.

I did some testing and found that when using ArrayAppend() to add a Structure to an array, that it overwrites all of the entries in the Array to match the entry it just added.

here is a section of my code

<cfset StreetArray = ArrayNew(1)>

<cfloop query="Streets">

    <cfset StreetRow['Street'] = STREET_NAME>

    <cfset StreetRow['Range'] = LOW_RANGE & ' - ' & HIGH_RANGE>

    <cfif EVEN_ODD is "E">

          <cfset StreetRow['EvenOdd'] = "Even">

    <cfelseif EVEN_ODD is "O">

          <cfset StreetRow['EvenOdd'] = "Odd">

    <cfelse>

          <cfset StreetRow['EvenOdd'] = "Both">

    </cfif>

    <cfset StreetRow['City'] = Streets.CITY>

    <cfset StreetRow['Zip'] = ZIP>

    <cfset StreetRow['Elementary'] = ELEMENTARY_SCHOOL>

    <cfset StreetRow['Middle'] = MIDDLE_SCHOOL>

    <cfset StreetRow['High'] = HIGH_SCHOOL>

    <cfset ArrayAppend(StreetArray, StreetRow)>

    <cfdump var="#StreetRow#">

    <cfdump var="#StreetArray#">

</cfloop>

Now i added the cfdumps on the end for testing.Here is what I get after the dumps.

bug.png

As you can see the StreetRow variable is updating properly, but after the second ArrayAppend() both entries in the array are the same even though the array was correct the first time.

I would like to also note that this does not happen if I explicitly create the structure inside the ArrayAppend() function so:

<cfset ArrayAppend(StreetArray, {Street=STREET_NAME,Range=LOW_RANGE & ' - ' & HIGH_RANGE, City=Streets.CITY,Zip=ZIP,Elementary=ELEMENTARY_SCHOOL,Middle=MIDDLE_SCHOOL,High=HIGH_SCHOOL,EvenOdd=EvenOdd})>

does not create the same issue.

This appears to be some kind of Bug, unless I am missing something...

It looks like ColdFusion is waiting until the loop is done before calling ArrayAppend() for all the loops. This works fine if you explicitly create the structures inside the function, but not so much if you are using a variable.

Complex datatypes, such as structures, are passed by reference.  In your loop you are repopulating the same structure with information.  When you append to the array, you are appending a reference to the structure. 

Add <cfset StreeRow = StructNew()> at the beginning of the loop.

Views

422

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jan 27, 2016 0
Engaged ,
Jan 27, 2016

Copy link to clipboard

Copied

Complex datatypes, such as structures, are passed by reference.  In your loop you are repopulating the same structure with information.  When you append to the array, you are appending a reference to the structure. 

Add <cfset StreeRow = StructNew()> at the beginning of the loop.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 27, 2016 1
New Here ,
Jan 27, 2016

Copy link to clipboard

Copied

That fixed it.

But this code worked before, is this something new?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 27, 2016 0
Engaged ,
Jan 27, 2016

Copy link to clipboard

Copied

Nothing new.  Not sure why this worked before unless you only had one street in the query 🙂 


As proof, here's a link to an old article from 2010 of someone else having the same issue.   Appending to array within a loop issue - Get Started - The SitePoint Forums

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 27, 2016 0
Birdwing LATEST
New Here ,
Jan 27, 2016

Copy link to clipboard

Copied

Or i just never noticed it

I'm the only webmaster/web developer here in charge of all the websites and internal web applications.


It's a lot to keep track of sometimes

anyway thanks for the help. I figured it was probably either a bug or me missing something simple.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 27, 2016 0