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  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Jan 16, 2007 at 9:50 AM // reply »
45 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 »
7,572 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
Mar 20, 2010 at 12:07 PM
Drawing On The iPhone Canvas With jQuery And ColdFusion
Simply awesome. Saved my day. ... read »
Mar 20, 2010 at 9:00 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
I would like to say thx for an easy way to create a bottom bar. I do have a ?. Is it possible to center the bar if i want to resize it to ex 85%. Regards Offenbach ... read »
Mar 19, 2010 at 7:26 PM
MySQL 3/4 - com.mysql.jdbc.Driver And allowMultiQueries=true
Thank you very much for this post. Adding allowMultiQueries="true" in context.xml didn't help until I added it to url as allowMultiQueries=true Good idea is to use prepared statements and it will he ... read »
Jim
Mar 19, 2010 at 4:49 PM
Nobody Puts Baby In The Corner!
Wow. This is like suddenly finding a support group for your secret shame. I'm not alone! I always liked this movie, even though it is extremely cheesy. I just wish Jennifer Grey hadn't gotten the ... read »
Mar 19, 2010 at 4:47 PM
Application.cfc OnRequest() Method Affects OnError() Arguments
@Jason and @Ben, I've been doing some CF9 refactoring on our systems and noticed an odd occurrence with onError as well. Found a way to work around my problem, but what I saw was... Background: Our ... read »
Jim
Mar 19, 2010 at 4:44 PM
Shoot 'Em Up Starring Clive Owen And Paul Giamatti
I actually enjoyed this movie quite a lot. It was different, certainly, but I think they were going for more of a Quentin Tarentino-"wow, that was weird"-vibe than an actual spoof. Once I realize ... read »
Mar 19, 2010 at 4:34 PM
An Intensive Exploration Of jQuery With Ben Nadel (Video Presentation)
Hey I guess the video is down. Is there anyway you can upload to youtube or vimeo or some other service? Greatly appreciated. ... read »
Mar 19, 2010 at 4:24 PM
ColdFusion CFPOP - My First Look
@Ben Thanks for the follow up! The root of the problem had to do with being able to trace bounced emails to specific records in a DB table. Let's say you run an email campaign and you get 1,000 bou ... read »