Different Use-Style Of FOR Loop In ColdFusion CFScrpit Tag

Posted January 16, 2007 at 8:38 AM

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:

 Launch code in new window » Download code as text file »

  • <!--- 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:

 Launch code in new window » Download code as text file »

  • <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.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Reader Comments

Jan 16, 2007 at 9:50 AM // reply »
35 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 »
6,516 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 Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »