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 />
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:
- // Create an Iterator for the array and loop over it.
- for (
- objIterator = arrParts.Iterator() ;
- objIterator.HasNext() ;
- WriteOutput( objIterator.Next() & "<br />" );
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.
Looking For A New Job?
- ColdFusion Developer Needed at AutoConX Systems
- 100% Remote - Sr ColdFusion Developer at Short's Travel Management
- ColdFusion Developer Opportunity at Cavulus
I am reading this book as well and what a great book it is. I would like to share thoughts on the book sometime!
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.