tracker issue : CF-3341256

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

CFLOOP needs an implicit index

| View in Tracker

Status/Resolution/Reason: Closed/Fixed/

Reporter/Name(from Bugbase): Nathan Strutz / Nathan Strutz (Nathan Strutz)

Created: 10/04/2012

Components: Language

Versions: 10.0

Failure Type: Enhancement Request

Found In Build/Fixed In Build: Final /

Priority/Frequency: Trivial / Unknown

Locale/System: English / Platforms All

Vote Count: 1

Listed in the version 2016.0.0.297996 Issues Fixed doc
Verification notes: verified_fixed on September 21, 2015 using build 2016.0.01.298513
When I am looping over something, such as a file, with cfloop, I often need a loop index. This either forces me to create a manual index, adding +1 on each iteration, or I have to change the type of loop to a for loop with an explicit index. It would be a great benefit if I there were a variable, such as #cfloop.index#, that would always reference the current-scoped loop, and would disappear when out of the loop scope.

Code example:

<cfset myArray = ["a","b","c"]>
<cfloop array="#myArray#" index="myArrayValue>
  Number #cfloop.index# value is #myArrayValue#<br>

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

Watson Bug ID:	3341256

External Customer Info:
External Company:  
External Customer Name: mrnate
External Customer Email:  
External Test Config: My Hardware and Environment details: Any



DEFINITELY. Rather than hard-coding the variable though in a pseudoscope, I'd recommend simply being able to specify an additional attribute on the CFLOOP like <cfloop array="#myArray#" index="i" element="eachElementValue"> Note, I am re-taking the INDEX attribute in this example to mean what it's supposed to, which fixes another bug in this tag. The INDEX should be the counter, and the ELEMENT should be the value at each element. As the attribute combo here is unambiguous, it should be possible to have this as well as the existing behaviour (which should be deprecated now, and obsoleted in CF11), Also agree that all other forms of loop should have an index attribute. It's almost always necessary or at least helpful. -- Adam
Vote by External U.
17750 | October 04, 2012 04:04:00 PM GMT
try coding it like this <cfset myArray = [{number = "one"}, {number = "two"}, {number = "three"}, {number = "four"}] /> <cfloop array="#myArray#" index="arrayItem"> <cfdump var="#arrayItem#" /> <cfdump var="#myArray.indexOf(arrayItem)#" /> <br /> </cfloop>
Comment by External U.
17744 | January 10, 2013 11:32:08 AM GMT
As part of another enhancement cfloop behavior has been modified to respect this scenario. A new attribute "item" is introduced in array cfloop, Consider these scenarios: 1. item and index, both are present, item will hold the element and index will hold index. 2. only item is present, item will hold the element. 3. only index is present, index will hold the element(to support backward compatibility) So you can code like this: <cfloop array=['a','b'] index="i" item="elem"> <cfoutput>#elem#</cfoutput> <cfoutput> #i#</cfoutput><br> </cfloop> The output will be as follows: a 0 b 1
Comment by Milan C.
17745 | November 27, 2014 12:34:42 AM GMT
Hi Milan, This ticket (CF-3321646) was created on Thursday, October 4, 2012 and is actually the duplicate of CF-3321646 which was created earlier on Thursday, August 30, 2012. So discussion should continue in CF-3321646, instead of splitting discussion over to here. Thanks!, -Aaron
Comment by External U.
17746 | September 05, 2015 11:15:23 PM GMT
This fix will be available in the next major version of ColdFusion. Thanks, Suchika.
Comment by Suchika S.
17747 | September 21, 2015 06:57:24 AM GMT
Hi Milan, Regarding your comment on 10:04:42 PM GMT+00:00 Nov 26, 2014: ---------------------- The output will be as follows: a 0 b 1 ---------------------- I've confirmed that the result is actually: ---------------------- a 1 b 2 ---------------------- And that would be the correct result. BTW, thanks for implementing this! Thanks!, -Aaron
Comment by External U.
17748 | September 21, 2015 02:07:33 PM GMT
I've verified the example also works for lists: <cfloop list="a,b" index="i" item="elem"> <cfoutput>#elem#</cfoutput> <cfoutput> #i#</cfoutput><br> </cfloop> Returns: a 1 b 2 Thanks!, -Aaron
Comment by External U.
17749 | September 21, 2015 02:15:05 PM GMT