Copy link to clipboard
Copied
I have just written a cfloop with array="ArrMyArray" index="i" as the attributues for the first time in a long time. In the loop, #i# actually refers to the content of the current array element, which seems wrong. Surely #i# should be the index/counter? If I want a counter, do I have to make one manually? We upgraded from CF7 to CF9 about a year ago, surely this isn't different in those versions? As far as I remember, when a cfloop has an index attribute, it is basically asking you what var name you want for the counter which holds the number of the current loop iteration?
Thanks to anyone who can shed any light on this.
T
Copy link to clipboard
Copied
Looping over arrays like that was only introduced into CF in CF8, so you would not have had that code in your CFMX7 code.
The implementation is flawed, as you point out: the index should be the index, not the value. This was pointed out to Adobe but they have chosen to ignore it.
--
Adam
Copy link to clipboard
Copied
Just tested it in 8.0.1 and agree it's wrong. The "item" attribute I'd expect to be the value, but not index="".
Copy link to clipboard
Copied
I disagree with Adam.
The implementation of looping through arrays is consisitent with the implementation of looping though a list. The index returns the value, not the counter.
For tomComcar, what you probably remember about looping through arrays where the index referred to the counter is that the cfloop tag also had to and from attributes.
Copy link to clipboard
Copied
I disagree with Dan's disagreement with Adam's response.
The *implementation* might be standard, but specifically the word "index" has always implied a 1- or 0-rooted integer representing the *position*, not the value itself. That's called a value.
Copy link to clipboard
Copied
I disagree with Dan's disagreement with Adam's response.
I agree with... aah... someone. But I probably disgaree with most people, at any given time.
The usage of index in a list loop is just as wrong and for the same reasons as it's wrong in the context of arrays. However it's more wrong for arrays because "index" already has meaning in the context of arrays, and the index is the number denoting which element is which, not the value of each element.
One could easily position it that in such a loop the index is the number, and the item (as per a collection loop) is the value. Obviously in a collection loop the "item" value is actually the index, but... oh, I give up. I really think that sometimes Adobe don't think things through as much as even we have already just on this thread.
--
Adam
Copy link to clipboard
Copied
I agree with Dan, looping through arrays is consistent with looping through lists. For any hard core traditionalists, or more precisely, hard code minimalists out there, you should only be using cfloop as an "indexloop" with the index, from, and to parameters. <cfloop index="i" from="1" to="#arrayLen(a)#">... would be the "non-flawed" version of a loop -- why would you use anything else if minimal is your cup of tea? Myself, I like having the option of looping through lists and arrays just how it is and always having the traditional indexloop option available for times when I need the counter.
Copy link to clipboard
Copied
I don't think anyone's disagreeing with the different methods, just that the word "index" is a reserved word which means the integer position of a value within a collection, and so should not be used to point to a value.
This is fine:
for ( i=1; i<= arrayLen(a); i++ ) {
a
}
This is also fine:
foreach ( string str in a ) {
str ;
}
But using what is essentially foreach loop with the word "index" is just wrong, the entire point of a foreach loop is that there's no index required.
Copy link to clipboard
Copied
I agree with Dan, looping through arrays is consistent with looping through lists. For any hard core traditionalists, or more precisely, hard code minimalists out there, you should only be using cfloop as an "indexloop" with the index, from, and to parameters. <cfloop index="i" from="1" to="#arrayLen(a)#">... would be the "non-flawed" version of a loop -- why would you use anything else if minimal is your cup of tea? Myself, I like having the option of looping through lists and arrays just how it is and always having the traditional indexloop option available for times when I need the counter.
I don't think you're getting the gist of the conversation. No-one's saying looping over arrays like that is a bad idea. They're just saying the index attribute is poorly named because it doesn't contain the array's index. It's containing the element at that index's value.
--
Adam
Copy link to clipboard
Copied
I would consider that a design decision as opposed to a flaw. cfloop has 6 different modes of using it and I would argue that consistently using "index" as the counter or item is less of a learning curve than a variety of parameter names depending on the loop type. They chose to use cfloop with 6 different modes as opposed to different looping tags, which is your example. Instead of reusing the cfloop tag, they could have chosen cfloop for 1 to x iteration, cfarrayloop for array looping, cflistloop, cfqueryloop, etc., and all with different and more descriptive parameters. But I would further argue that using the single cfloop tag with 6 different modes, albeit with parameters that aren't 100% descriptively accurate, created a language with a shorter learning curve than if they would have went the individual six different looping tags route.
Copy link to clipboard
Copied
created a language with a shorter learning curve than if they would have went the individual six different looping tags route.
Except in the case under discussion here wherein the natural assumption will be that "index" in the context of an array, means "the index". Not the value. I think even in the context of <cfloop> one would think "the loop counter" not the value of the element the loop index is pointing to. "index" means something: Adobe don't get to use it in a different context. Wel;l they oughtn't
There's a precedent set: an index/to/from loop where the index is a number (although arguably it's the value too!), and there's a precedent set in a item/collection loop that the index is described as an item. The only precedent set where the index is the actual value is when it's an indexed loop. And, as I said, the value here is just the current index; there is no value other than that. The value is then used as an index on something else.
I don't think there's been a precedent of coherence set at all here, so:
* at least if they stuck with the precedent set by array nonemclature there'd be some coherence to something;
* it doesn't surprise me we ended up with what we got.
And, again, no-one is arguing for six different tags are they? That's all in your head.
--
Adam
Copy link to clipboard
Copied
I'm just saying that consistently using index, albeit in a non-grammatically correct way, is a better design choice than some of the alternatives. To me, a bigger issue is that using cfloop for a queryloop does not have group support -- now that is a flaw to me!
BTW, that's now the only arguments taking place in my head. There is also coffee or bathroom? Get back to work or stay on soapbox? Other threads...
Copy link to clipboard
Copied
To me, a bigger issue is that using cfloop for a queryloop does not have group support -- now that would be a flaw to me!
You'll be pleased about CF10 then... 😉
BTW, that's now the only arguments taking place in my head. There is also coffee or bathroom? Get back to work or stay on soapbox? Other threads...
Chuckle.
--
Adam