CFLoop Attributes Evaluated Only Once

Posted July 22, 2006 at 6:23 PM

Tags: ColdFusion

For index loops in ColdFusion that are looping over an array or list, I always put the ArrayLen() or ListLen() in the TO attribute. I always felt a little uneasy about it because I thought maybe the function was getting evaluated for each iteration of the index loop. My fear was that this was adding additional processing overhead for each iteration.

Well, I finally got my out of shape butt off the proverbial couch and actually tested what was going on:

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

  • <cfoutput>
  •  
  • <!--- Create an initial array of items. --->
  • <cfset arrItems = ListToArray( "ben,dave,mark,mike" ) />
  •  
  • Original Length: #ArrayLen( arrItems )#<br />
  • <br />
  •  
  • <!---
  • Loop over the items in the array. For each iteration, add
  • an item to the array to see if the loop will keep going.
  • In the TO attribute, use the Min() method to ensure that
  • the loop doesn't go on infinitely.
  • --->
  • <cfloop
  • index="intIndex"
  • from="1"
  • to="#Min( 10, ArrayLen( arrItems) )#"
  • step="1">
  •  
  • <!--- Output the iteration values. --->
  • Iteration #intIndex#. Array Length: #ArrayLen( arrItems )#<br />
  •  
  • <!--- Add an Item to the array to see if we keep looping. --->
  • <cfset ArrayAppend(
  • arrItems,
  • "sarah"
  • ) />
  •  
  • </cfloop>
  •  
  • </cfoutput>

Turns out, the loop only iterates 4 times; the attributes only get evaluated that first time. When you stop to think about it, that makes a lot of sense; it is just a string. And if you take Conditional loops into account, it makes sense that the condition="" attribute is a string, not an evaluation. That way it can do a sort of delayed evaluation on the condition.

Man, understanding the basics is so important.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Print Page




Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 19, 2010 at 5:39 AM
Regular Expressions Make CSV Parsing In ColdFusion So Much Easier (And Faster)
I get a java heap error on a 4000 line x 8 col csv file. (Processing only, not doing anything with the result yet.) If I cut the file in half it works. Why does it use so much memory? Can anything ... read »
Mar 19, 2010 at 2:29 AM
URL Rewriting And ColdFusion's WriteToBrowser Image Functionality (CFFileServlet)
Pakistan Travel information for attractions in Pakistan, Balochistan, Sindh, NWFP and Punjab. Gilgit-Baltistan guide, history and culture. ... read »
Mar 19, 2010 at 1:43 AM
jQuery Attr() Function Doesn't Work With IMAGE.complete
sample: ..... var loadWatch = setInterval(function() { if(img.complete) { clearInterval(loadWatch); completeCallback(img); } }, 100); } else .... ... read »
Mar 19, 2010 at 12:50 AM
jQuery Attr() Function Doesn't Work With IMAGE.complete
I just fixed the code. There was a function "watch" inside the function imgLoad. It spammed a lot of errors: Error: missing argument 1 when calling function watch. To fix this: instead of setInt ... read »
Mar 18, 2010 at 10:28 PM
Posting XML SOAP Requests With jQuery
can you please point me to the jquery documentation on the following # // Create our SOAP body content based off of # // the template. # var soapBody = soapTemplate.html().replace( # new RegExp( "\\ ... read »
Mar 18, 2010 at 6:34 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Ben Very useful analyses. Thank you @Elliot Thanks for additional clarification Though, it's quite a shame that getBust() failed...not defined ;) ... read »
Mar 18, 2010 at 5:35 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
Saving private properties is necessary so that you can "reconstitute" an object on the other side of the wire, or load up a serialized object you saved to disk. If it didn't save the private state o ... read »
Mar 18, 2010 at 4:04 PM
jQuery's Event Triggering, Order Of Default Behavior, And triggerHandler()
Tks! You saved-me today. it can be chained into one statement: $("#x).attr("checked","checked").triggerHandler('click'); ... read »