tracker issue : CF-3321646

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

Deprecate CFLOOP/array and try again

| View in Tracker

Status/Resolution/Reason: Closed/Withdrawn/Duplicate

Reporter/Name(from Bugbase): Adam Cameron / Adam Cameron (Adam Cameron)

Created: 08/30/2012

Components: Language

Versions: 10.0

Failure Type:

Found In Build/Fixed In Build: Final / 292325

Priority/Frequency: Major / Some users will encounter

Locale/System: English / Win All

Vote Count: 3

Duplicate ID:	CF-3341256

Problem Description:
Looping over arrays with <cfloop> is a bit munged.  The construct is <cfloop index="element" array="array">.  Note that I observe the problem here: the INDEX attribute doesn't actually hold the index, it holds the element itself.  And the real index value is not actually exposed at all.  I would say the bulk of the time one would want the index as much as one would want the element; and at least if one has the index then one can derive the element (not the other way around though).  

Can I suggest you deprecate that, and implement this:
<cfloop index="index" element="element" array="array">

This would expose both the index and the element, and would use the correct name for each.  Plus would be backwards compat with the existing incorrect one (in that both could still work fine).

Filing as a bug not an E/R as the index/element thing in the existing behaviour is a bug.

Cheers.

-- 
Adam

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

Watson Bug ID:	3321646

External Customer Info:
External Company:  
External Customer Name: Adam Cameron.
External Customer Email:  
External Test Config: My Hardware and Environment details:

Attachments:

Comments:

Actually, please implement it the same as Railo already has: <cfprocessingdirective pageencoding="utf-8"> <cfset daysOfWeek = ["R?hina","R?t?","R?apa","R?pare","R?mere","R?horoi","R?tapu"]> <cfloop array="#daysOfWeek#" index="index" item="day"> <cfoutput>#index#: #day#<br /></cfoutput> </cfloop> <hr /> <cfset daysOfWeek = {monday="R?hina", tuesday="R?t?", wednesday="R?apa", thursday="R?pare", friday="R?mere", saturday="R?horoi", sunday="R?tapu"}> <cfloop collection="#daysOfWeek#" index="index" item="day"> <cfoutput>#index#: #day#<br /></cfoutput> </cfloop> <hr /> <cfset daysOfWeek = "R?hina,R?t?,R?apa,R?pare,R?mere,R?horoi,R?tapu"> <cfloop list="#daysOfWeek#" index="index" item="day"> <cfoutput>#index#: #day#<br /></cfoutput> </cfloop> <hr />
Comment by External U.
18227 | August 31, 2012 02:30:30 AM GMT
+1, index should hold the index (it should do what it says)
Vote by External U.
18242 | September 23, 2012 12:58:49 AM GMT
Personally no Adam... That is why there is a for loop, called cfloop from="" to="" the other way is more for looping and getting that record out of the array, so if you need to know the position then use the other method that is why it is there in the first place. Having multiple functions / tags that do the same thing is something I am not in favor for.
Comment by External U.
18228 | January 10, 2013 08:45:19 AM GMT
Why can't people just use this instead <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.
18229 | January 10, 2013 11:35:48 AM GMT
I honestly don't understand why this was called 'index' in the first place when it was intended to be the item. :-( The backward compatibility is going to be an issue here. changing index to behave like real index will break a lot of code. Possibly here is what we can do - Always honor 'item' if it is there - if 'index' is the only attribute, then retain the old behavior - if 'index' and 'item' both are specified, no issues thoughts?
Comment by Rupesh K.
18230 | May 02, 2014 09:33:33 PM GMT
Yep, that's a good solution IMO. Cheers Rupesh. -- Adam
Comment by External U.
18231 | May 03, 2014 03:25:39 AM GMT
Hi Rupesh, Awesome, sounds good to me too. Thanks!, -Aaron
Comment by External U.
18232 | May 06, 2014 12:40:19 AM GMT
Needed immediately. Adding counters is ridiculous and adds to code bloat.
Vote by External U.
18243 | August 19, 2014 03:20:52 PM GMT
By the by, I've just noticed the same situation exists for list looping. Is that being fixed in a similar way?
Comment by External U.
18233 | December 01, 2014 10:54:45 AM GMT
Railo kept the name as "item" instead of "element" when they did the same, but I agree with Adam nonetheless.
Vote by External U.
18244 | June 30, 2015 03:39:47 AM GMT
Hello?
Comment by External U.
18234 | July 09, 2015 04:20:29 AM GMT
Adobe, you set the "Duplicate ID" on the wrong ticket and then continued this discussion in the duplicate ticket. _This_ ticket (CF-3321646), created on Thursday, August 30, 2012, is the original ticket. Ticket CF-3341256, created on Thursday, October 4, 2012, is the duplicate ticket. Please mark CF-3341256 as a duplicate of CF-3321646 and continue the discussion here. Here is Adobe's comment from the duplicate ticket: ------------------------------------------------------------------------- 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 ------------------------------------------------------------------------- Shouldn't that output be?: a 1 b 2 Thanks!, -Aaron
Comment by External U.
18235 | September 05, 2015 11:04:32 PM GMT
Can someone please answer Adam's question? He asked it on Dec 1, 2014. List looping also needs to be fixed. Will this.. <cfloop list="a,b" index="i" item="elem"> <cfoutput>#elem#</cfoutput> <cfoutput> #i#</cfoutput><br> </cfloop> ..return this: a 1 b 2 Thanks!, -Aaron
Comment by External U.
18236 | September 05, 2015 11:09:46 PM GMT
Hi Aaron & Adam, List looping is fixed. We intend to make this fix available in the next major version of coldfusion. Thanks, Suchika.
Comment by Suchika S.
18237 | September 21, 2015 06:26:05 AM GMT
Thanks Suchika! -Aaron
Comment by External U.
18238 | September 21, 2015 02:00:57 PM GMT
This needs to be reopened. One of the important parts of this is *deprecating the previous syntax*, ie: <cfloop array="" index="">. This is essential, as otherwise there are two supported syntaxes for array-element looping, which is... daft.
Comment by External U.
18239 | February 29, 2016 02:55:52 AM GMT
Hi Adobe, +1 to what Adam said. It's best to flag the old <cfloop array="" index=""> syntax as deprecated so that new code doesn't use it. Thanks!, -Aaron
Comment by External U.
18240 | February 29, 2016 07:55:38 AM GMT
This ticket shouldn't have been closed until <cfloop array="" index=""> was marked deprecated, b/c (again, and like Adam said) that was part of this ticket. It shouldn't require a new ticket. So this ticket should be re-opened. Thanks!, -Aaron
Comment by External U.
18241 | February 29, 2016 07:57:39 AM GMT