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

Dan
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,371 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 7, 2009 at 5:53 PM
Ask Ben: Javascript String Replace Method
You can find here an advanced function that prepared with javascript replace function. This can make the first letters of words, sentences, lines and whatever you define automatically: http://www.m ... read »
Andrew Neely
Nov 7, 2009 at 4:56 PM
A Moment That Touched Me - The Fountainhead
Ben, Glad you enjoyed the podcast. Yeah, the Tank Riot guys can get really chatty during the episodes, but that's part of the charm of it for me. They've covered everything from Nichola Tesla to Cha ... read »
Nov 7, 2009 at 4:43 PM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
Is it possible to make some more MenĂ¼`s ? ... read »
Jill
Nov 7, 2009 at 11:40 AM
How To Unformat Your Code (Like A Pro)
Derek, I think you might be right - sweet! Thanks for the link :) ... read »
Nov 7, 2009 at 11:25 AM
How To Unformat Your Code (Like A Pro)
I think it would be way easier to just use this http://www.logichammer.com/html-formatter/ He just released v3 and it rocks. ... read »
Jill
Nov 7, 2009 at 7:58 AM
How To Unformat Your Code (Like A Pro)
LMAO - this was pretty funny! I have to admit - I also love to reformat code so I can read it. My boss used to tell me to leave my OCD at home. Now I don't feel so bad after reading everyone else' ... read »
Nov 6, 2009 at 10:10 PM
How To Unformat Your Code (Like A Pro)
The timing of this post is just uncanny. I spent the last 15-20 minutes manually un-formatting my "Ben Nadel" style code within a CFC of mine. I was really digging the readability a few weeks ago, bu ... read »
Roe
Nov 6, 2009 at 5:11 PM
Passing Arrays By Reference In ColdFusion - SWEEET!
ArraySort also reorders the results of these java obj's ... read »