If you use a FOR loop rather than <cfloop>, you *can* adjust your index @ whim.
-- Adam On Jun 15, 6:09 pm, Victor Balada Diaz <[email protected]> wrote: > Thanks a lot Alan, you're right. I think i oversimplified my example. > > The problem is that once you start nesting loops and try to make it more > dynamic code gets ugly quite fast. Eg, this is a bit more complex with > just one nested loop: > > I have a set of boxes sorted by color and i want to do something with > each color. The array is sorted by color so this algorithm should > work. > > <cfloop index="x" from="1" to="ArrayLen(boxes)"> > <cfset samecolorboxes =ArrayNew(1)/> > <cfset ArrayAppend(samecolorboxes, boxes[x])/> > > <!--- If there is more than one box with the same color ---> > <cfif x LT ArrayLen(boxes) AND boxes[x+1].color EQ boxes[x].color> > <cfloop index="y" from="x+1" to="ArrayLen(boxes)"> > <cfif boxes[y].color EQ boxes[y-1].color> > <cfset ArrayAppend(samecolorboxes,boxes[y])/> > <cfelse> > <cfset x=y/> > <cfbreak/> > </cfif> > </cfloop> > </cfif> > <cfset DoSomething(samecolorboxes)/> > </cfloop> > > Of course i could just do: > > <cfset samecolorboxes = ArrayNew(1)/> > <cfloop index="x" from="1" to="ArrayLen(boxes)"> > <cfset ArrayAppend(samecolorboxes, boxes[x])/> > <cfif x is arraylen(boxes) or boxes[x].color is not boxes[x+1].color > > <cfset DoSomething(samecolorboxes)/> > <cfset samecolorboxes = ArrayNew(1)/> > </cfif> > </cfloop> > > But i'm trying to show an example of an use case where it might be > useful to modify an index variable or skip more than one iteration > on a nested loop. > > I'm a sysadmin with C skills and i'm learning CFML right now, so i > might be trying to do something in a very complex way that's easily > solvable in other way in CFML, but i just find weird that i'm not able > to modify index values. Is there any way of modifying index values or > some reason for not being able to do it? > > Thanks a lot. > Regards. > > On Tue, 2010-06-15 at 17:04 +0100, Alan Williamson wrote: > > yes you can .... > > > <cfloop index="x" from="1" to="50"> > > <cfoutput>#x# </cfoutput> > > > <cfif (x > 5) && (x < 20)> > > <cfcontinue> > > </cfif> > > </cfloop> > > > Victor Balada Diaz wrote: > > > Hello Alan, > > > > The problem with cfcontinue is that you can't skip in a clean way eg, 20 > > > iterations. There is no such thing as cfcontinue times="20" nor just an > > > easy way to switch to a fixed position in the index, so i can not > > > achieve what i'm trying to do with cfcontinue. Do you have any other > > > idea? > > -- Open BlueDragon Public Mailing List http://www.openbluedragon.org/ http://twitter.com/OpenBlueDragon online manual: http://www.openbluedragon.org/manual/ mailing list - http://groups.google.com/group/openbd?hl=en
