tracker issue : CF-3328578

select a category, or use search below
(searches all categories and all time range)
Title:

cfloop array and wrong iterator

| View in Tracker

Status/Resolution/Reason: Closed/Fixed/

Reporter/Name(from Bugbase): Jork Zijlstra / Jork Zijlstra (Jork Zijlstra)

Created: 09/11/2012

Components: Language

Versions: 9.0.1

Failure Type:

Found In Build/Fixed In Build: 9.0.1 / 3328578

Priority/Frequency: Major / All users will encounter

Locale/System: English / Windows 7

Vote Count: 0

Problem Description:
When using cfloop array the index should contain the item in the array. When you var scope the index anywhere in your function the index item will always be the first item of the array.

I suspect that since array are nog passed by reference this is a scoping issue.

Steps to Reproduce:

Create a component with the folling:
<cfcomponent>
	
	<cffunction name="test" returntype="void" access="public" output="false">
	
		<cfset test_arr = ["aaa", "bbb"] />
		<cfdump var="#test_arr#">
		<cfloop array="#test_arr#" index="item_str">
			
			<cfdump var="#item_str#">
		</cfloop>

		<!--- Put the next line in comment to have a working loop --->
		<cfset var item_str = "" />
		<cfabort>
		
	</cffunction>
</cfcomponent>

And call the function test()

Actual Result:
2x times an output of "aaa" and "aaa"

Expected Result:
output of "aaa" and "bbb"

Any Workarounds:
1) Use cfscript with a for(item_str in test_arr)
2) Don't var scope the idex in the function when tag based is needed

----------------------------- Additional Watson Details -----------------------------

Watson Bug ID:	3328578

External Customer Info:
External Company:  
External Customer Name: Jork Zijlstra
External Customer Email:  
External Test Config: My Hardware and Environment details:

Windows 7, CF 9.0.1 multiserver

Attachments:

Comments:

I agree this is a bug, but the easiest work around is just to declare your VAR scoped variable BEFORE you use it, not afterwards ;-) Still: CF is def doing something weird here. -- Adam
Comment by External U.
18104 | December 22, 2013 05:00:46 AM GMT
Just out of interest, Adobe peeps, what was the underlying problem here? -- Adam
Comment by External U.
18105 | February 11, 2014 02:58:29 AM GMT
We where used to reuse variable names, but aren't doing this now. When using cfloop we expected the index property to be scoped within the loop itself. This is evidently not the case for Coldfusion.
Comment by External U.
18106 | February 11, 2014 04:46:31 AM GMT