tracker issue : CF-3827490

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

Implementation omission in for(element in list) loop

| View in Tracker

Status/Resolution/Reason: Closed/Won't Fix/Workaround

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

Created: 09/18/2014

Components: Language

Versions: 10.0

Failure Type:

Found In Build/Fixed In Build: Final /

Priority/Frequency: Minor / Some users will encounter

Locale/System: English / Platforms All

Vote Count: 2

Repro:

{code}
for (element in "tahi,rua,toru,wha"){
	writeOutput("#element#<br>");
}
{code}

That's fine.

Now, what if I have this:
{code}
for (element in "tahi;rua;toru;wha"){ // the delimiter is now a semi-colon
	writeOutput("#element#<br>");
}
{code}

The docs for CFScript are shockingly bad, so it's either not documented at all, or I just can't find 'em.

Anyway, any list operation needs to support the specification of alternative delimiters. I suggest this:

{code}
for (element in "tahi;rua;toru;wha"; ";"){ 
	writeOutput("#element#<br>");
}
{code}

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

Watson Bug ID:	3827490

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

Attachments:

Comments:

(oh... obviously affects CF11 too)
Comment by External U.
10859 | September 18, 2014 07:51:42 AM GMT
Not really a bug, in my opinion. The syntax for for-in loops is: for (someKey in someStructure) { } The fact that your example works with a list is accidental. Nevertheless, you may request it as a new feature to extend the language.
Comment by External U.
10860 | September 18, 2014 08:12:04 AM GMT
Oh, a quick search for a reference, found: http://help.adobe.com/en_US/ColdFusion/10.0/Developing/WSc3ff6d0ea77859461172e0811cbec0a0e0-7fdf.html#WSc3ff6d0ea77859461172e0811cbec0a0e0-7fe8
Comment by External U.
10861 | September 18, 2014 08:15:32 AM GMT
Your knowledge of CFML (in this regard) is as out of date as those docs are, BKBK.
Comment by External U.
10862 | September 18, 2014 08:19:41 AM GMT
Adam, whenever you step in the ring with me, you invariable throw the first punch, miss, and never seem to get your jab going. You will find you are wrong on this occasion, too. Firstly, Adobe has at least published some unequivocal ColdFusion 11 documentation on the for-in loop. You will find it on: https://wikidocs.adobe.com/wiki/download/attachments/142314172/ColdFusion11_Developing.pdf. Secondly, the documentation on the for-in loop has hardly changed between ColdFusion 10, which you call out of date, and ColdFusion 11. I will quote from the ColdFusion 11 documentation (see page 230 of the PDF): "The for-in loop loops over the elements in a ColdFusion structure. It has the following format: for (variable in structure) statement The variable can be any ColdFusion identifier; it holds each structure key name as ColdFusion loops through the structure. The structure must be the name of an existing ColdFusion structure. The statement can be a single semicolon terminated statement or a statement block in reference." In fact, the documentation even sounds this note of caution at the end: "Unlike the cfloop tag, CFScript for-in loops do not provide built-in support for looping over queries and lists.".
Comment by External U.
10863 | September 18, 2014 09:39:41 AM GMT
Read 'invariably' for 'invariable'
Comment by External U.
10864 | September 18, 2014 09:42:57 AM GMT
BKBK you would have a point if you were actually right. Which... you're not. Clearly Adobe *have* added functionality to loop over a list with for/in, because the above code WORKS. They just haven't updated the docs. Ergo: the docs are out of date. You also don't seem to be in possession of this knowledge, hence my observation that your own knowledge is - likewise - out of date. for/in has been updated to support looping over arrays (since CF9), queries (since CF10) and lists (also since CF10). I expect files to be added in the coming CF11 patch (https://bugbase.adobe.com/index.cfm?event=bug&id=CF-3754577, see Rupesh's second to bottom comment). None of this is mentioned in the docs (either the offline version you are using, or the online up to date ones), but this does not mean it's not reality. Which is where a lot of us reside.
Comment by External U.
10865 | September 18, 2014 10:10:41 AM GMT
Residing in reality? Remember where we are. You are reporting a bug, the document you refer me to is likewise a bug report, and you acknowledge regression in the documentation. That this is work in progress, changing as we speak, is undeniable. Check out the dates. That is my interpretation of all the for-in constructs other than the established "key in structure".
Comment by External U.
10866 | September 18, 2014 10:50:18 AM GMT
+1 - Ability to specify delimiter would be useful
Vote by External U.
10874 | November 24, 2014 04:54:30 AM GMT
I do agree with Rupesh. for-in syntax is too generic to push a specific list requirement. If really required, a list can be converted to an array first and then use it within for-in.
Comment by Awdhesh K.
10867 | November 27, 2014 04:19:06 AM GMT
Awdhesh. You. Already. Support. Lists. In. for-in. Loops. I dunno how I can make that any clearer. The problem is you've done an incomplete implementation. If you had not already *started* (and released) this work, then I'd agree. However you *have* started to implement it. So you need to *complete* the implementation. This isn't an open source project by a chap balancing his laptop on his knee in his bedroom. It's something you sell as *enterprise* software. List operations in CFML need to support delimiters (and empty elements as well). Not "when we've already done it", or "only when we didn't forget", and not omitted because "oh... [here's a convenient excuse]".
Comment by External U.
10868 | December 04, 2014 07:34:14 AM GMT
For-in syntax is a generic syntax for iterating over ALL collections. I don't like the idea of introducing variations here which apply only to one specific datastructure. What you are asking will make it messier - For list you would ask for "delimiter" and "includeEmptyField". For query, you would ask for startrow and end row. Do you see what I am saying. So, no - we are not doing it.
Comment by Rupesh K.
10869 | December 08, 2014 10:28:14 AM GMT
Cheers for the reply Rupesh. The query looping case you offer is not an appropriate parallel because a for-in should loop over the *entire* collection, not a subset. So - no - no-one would expect those options to be supported, as they break the rule of what for-in is supposed to do. If anything, The list situation is the *opposite* of that: it needs further parameters for it to even work. I cannot think of a similar situation to this one with lists with any other collection type in CFML, where the code defines what constitutes how the object is interpreted as a collection. So there is no existing precedent here, so falling back onto existing behaviour is an invalid approach to assessing the situation. But you demonstrate my point, in a way. for-in is for looping over a collection-type. However it currently *doesn't work*, for the rule that define what a list actually *is*. A list in CFML is a string delimited *by one or more individual characters*. It is not "delimited by a comma". That is just wrong. From the docs (https://wikidocs.adobe.com/wiki/display/coldfusionen/Data+types-+Developing+guide#Datatypes-Developingguide-Lists): "In ColdFusion, a list is just a string that consists of multiple entries separated by delimiter characters." As far as the "includeEmptyField" option goes: I think we could legitimately live without that. Consider this array equivalent: <cfset a = arrayNew(1)> <cfset a[1] ="a"> <cfset a[3] = "c"> <cfloop index="e" array="#a#"> <cfoutput>[#e#]</cfoutput> </cfloop> This outputs: [a][c] It only considers *existing* elements. Bottom line: you were wrong to hard-code that the delimiter is a comma, as that is simply incorrect. I also don't see what the big problem is having this syntax: for (element in list; delimiters) It's unambiguous for the parser, and is simple, logical and obvious from a code perspective. It's also in-keeping with *every single other list operation in the entire language*. Which should be part of the requirement here. Cheers.
Comment by External U.
10870 | December 08, 2014 12:17:08 PM GMT
Agreed with Adam. The implementation of looping over lists should be complete.
Vote by External U.
10875 | December 08, 2014 12:42:54 PM GMT
Oh wow. I got my code example wrong for arrays... forgot we're speaking about script. This would be the analogue for arrays: <cfscript> a = arrayNew(1); a[1] ="a"; a[3] = "c"; for (e in a){ writeoutput("[#e#]"); } </cfscript> And on ColdFusion (but not Railo), the for-in loop version is broken. Will raise a bug. This does not distract from my main point though.
Comment by External U.
10871 | December 08, 2014 01:13:53 PM GMT
New ticket is https://bugbase.adobe.com/index.cfm?event=bug&id=CF-3864256
Comment by External U.
10872 | December 08, 2014 01:19:08 PM GMT
Oh, and let's not forget you had no problem adding support for both delims and empty elements for the *other* "standard" collection iteration constructs such as each() and <cfloop>. So I think your case here is pretty bloody weak.
Comment by External U.
10873 | December 08, 2014 01:30:56 PM GMT