CFLoop Attributes Evaluated Only Once

Posted July 22, 2006 at 6:23 PM by Ben Nadel

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:

  • <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.



Reader Comments

There are no comments posted for this web log entry.

Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 21, 2013 at 9:25 AM
Turning Off and On Identity Column in SQL Server
you are awesome..i am lucky to get this blog between such a garbage one....Thanks, Prashant ... read »
May 20, 2013 at 4:38 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, Your confusion is well founded, since this is a very confusing features. In fact, it ONLY works if you use array notation. Meaning, that this: arrayToList( query[ "columnName" ] ) ... read »
May 20, 2013 at 4:34 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I was thinking chicken and the egg, I wouldn't have expected it to work in the valuelist going in I guess. Maybe I just need a beer, long day :) ... read »
May 20, 2013 at 4:29 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, That's if you're trying to reference a specific row. In this case, we're trying to reference the entire query column as one cohesive value. So, you are correct that if you wanted to output a ... read »
May 20, 2013 at 4:24 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I thought when you used array notation to reference queries you always had to have the row or it would throw a similar error as well? ... read »
May 20, 2013 at 11:45 AM
Using jQuery's Animate() Step Callback Function To Create Custom Animations
This is really useful. I found out that you don't actually have to use a dummy css property (surprisingly). To animate a property in a linear-gradient for instance I did this this.css('someLinearGra ... read »
May 20, 2013 at 10:51 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Josh, Oh snap! You're totally right! I'm not sure I've ever tried that. I did know that you can call a number of other array-methods on ColdFusion query columns: http://www.bennadel.com/blog/167 ... read »
May 20, 2013 at 10:45 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Ben - I believe you can achieve the same functionality with ColdFusion's built in ArrayToList() function. ArrayToList( users[ "id" ] ); ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools