Different Use-Style Of FOR Loop In ColdFusion CFScrpit Tag

Posted January 16, 2007 at 8:38 AM by Ben Nadel

Tags: ColdFusion

I was reading through Head First Object-Oriented Analysis and Design last night when I came across a neat little snippet of code that used the FOR loop to create an Iterator object and loop through this. This is something I have done in Javascript, but would have never thought to do in ColdFusion (or even thought that it would parse correctly).

When it comes to Iterator usage, I generally use this kind of While loop:

  • <!--- Create an array. --->
  • <cfset arrParts = ArrayNew( 1 ) />
  •  
  • <!--- Throw some random data in it to populate. --->
  • <cfset ArrayAppend( arrParts, "Ankle" ) />
  • <cfset ArrayAppend( arrParts, "Calves" ) />
  • <cfset ArrayAppend( arrParts, "Thighs" ) />
  • <cfset ArrayAppend( arrParts, "Butt" ) />
  • <cfset ArrayAppend( arrParts, "Boobs" ) />
  • <cfset ArrayAppend( arrParts, "Face" ) />
  •  
  •  
  • <!--- Create an iterator for the array. --->
  • <cfset objIterator = arrParts.Iterator() />
  •  
  • <!---
  • Keep getting the next object from the iterator until
  • will have reached the end of the array and have no
  • more items.
  • --->
  • <cfloop condition="objIterator.HasNext()">
  •  
  • #objIterator.Next()#<br />
  •  
  • </cfloop>

Notice that we have to create the Iterator object before the loop. The CFLoop tag is a bit more restrictive than it's CFScript counterpart. In CFScript (as I was reminded by the Head First book) you can do something like this:

  • <cfscript>
  •  
  • // Create an Iterator for the array and loop over it.
  • for (
  • objIterator = arrParts.Iterator() ;
  • objIterator.HasNext() ;
  • ){
  •  
  • WriteOutput( objIterator.Next() & "<br />" );
  •  
  • }
  •  
  • </cfscript>

Notice that the Iterator creation AND usage are both part of the FOR loop. Also notice that while there is a second ";" which is required, there is really no "incrementor" for the loop. That is because the incrementing doesn't take place in the FOR loop logic, but rather, it is done via the calls to Next().

Anyway, this is really minor and not functionally different in any way than a While loop, but I think it looks pretty tight.



Reader Comments

Jan 16, 2007 at 9:50 AM // reply »
51 Comments

I am reading this book as well and what a great book it is. I would like to share thoughts on the book sometime!


Jan 16, 2007 at 2:56 PM // reply »
11,314 Comments

Dan, most definitely. I am only on page 30, so it might be a while, but so far I am liking the typical "Head First" stylings.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Jun 19, 2013 at 10:18 AM
ColdFusion Path Usage And Manipulation Overview
Anyone happen to know if the file created by getTempFile will be automatically removed at any point? Nothing mentioned in the docs, and restarting CF doesn't remove them, so it seems it needs manu ... read »
Jun 19, 2013 at 9:41 AM
Working With Inherited Collections In AngularJS
I actually just ran into this same situation with a demo I was putting together. Your implementation of multi-lvl $scope's > Mine :) ... read »
Jun 19, 2013 at 8:17 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@Prateek, to match a word or text you should use .toContain('word') that's a jasmine reference. website is : http://pivotal.github.io/jasmine/ ... read »
Jun 19, 2013 at 8:10 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
Hi Guys, Actually i am doing e2e test of angular js of my project but i am not getting one thing that is how to press enter key through the test when my form is filled as i am not using a button but ... read »
Jun 18, 2013 at 9:20 PM
Mapping AngularJS Routes Onto URL Parameters And Client-Side Events
I couldn't find examples of passing multiple arguments using the when() routing statement so figured out through trial and error that you can pass multiple arguments using the following format: .whe ... read »
Jun 18, 2013 at 3:39 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
Hi Ben, THANKS! While not bleeding edge, it is new to me & I like learning new things every day! ... read »
Jun 18, 2013 at 12:30 PM
Disabling Auto-Correct And Auto-Capitalize Features On iPhone Inputs
Also spellcheck="false" should be mentioned as part of html5 specs ... read »
Jun 18, 2013 at 8:40 AM
Using Named Functions Within Self-Executing Function Blocks In Javascript
Hi Ben, you forgot to mention the most important thing for named self-executing functions - they can be referenced by name ONLY inside their execution context (which is parens in this case), it mean ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools