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



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

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 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »