Skip to main content

ColdFusion Tutorial - Looping over a list

This is a basic ColdFusion tutorial on how to loop over a list. It's pretty simple once you get the gist of it, but getting started can be tricky. Another reference can be found at Adobe LiveDocs.

Basically, there are five steps:

  1. Define the list
  2. Create a CFOUTPUT block
  3. Create a CFLOOP block inside the CFOUTPUT block
  4. Do something in the loop block
  5. Show the results
This tutorial will show you step-by-step what to do.

1. Define the list

To loop over a list, you must first have a list to loop over. A list is a combination of different or same-typed values. Usually, lists are separated with commas, but you can use anything to separate lists. I usually use a bell character, but the default is a comma.

NOTE: Make sure that you do not have any spaces between your list elements.

<cfset radioList = "one,two,three" />


2. Create a CFOUTPUT block

A CFOUTPUT block consists of an open and a close CFOUTPUT tag. This tells the server to interpret anything between those two tags as ColdFusion code, so any string surrounded by pound signs (#) will be interpreted as a variable. If the variable hasn't been defined, CF will let you know it by crashing.


<cfoutput>
</cfoutput>




3. Create a CFLOOP block

You can perform a loop over many different things, including collections like arrays and structures, lists, and queries to name a few. You can also loop over stuff coming from COM objects, but that's advanced stuff.

To loop over a list, you have to define what list you're looping over, and what variable you're going to use to refer to the position in the list (here called the "index").

NOTE: Make sure that you use the variable called radioList ("#radioList#") and not just the string "radioList", because CF doesn't know that "radioList" is a variable here unless you tell it.


<cfloop list="#radioList#" index="i">

</cfloop>



4. Do something in the loop block

Between the opening and closing CFLOOP tags, you may define what will be done with every item in the list. I'm setting a few variables in the attributes scope to zero if they don't exist, like this:

<cfparam name="attributes.#i#" default="0" />

NOTE: Because this is inside a CFOUTPUT block, the code will render like this:

<cfparam name="attributes.one" default="0" />
<cfparam name="attributes.two" default="0" />
<cfparam name="attributes.three" default="0" />

5. Show the results

One of the greatest things ever in any language, machine or human, is CFDUMP. No, it's nothing disgusting, it's actually quite beautiful. What it does is spit out a structure, no matter how simple or complex, in a human-readable format. This helps when you've got a structure that might have arrays nested in other arrays nested in structures n levels deep (ever try dumping application.fusebox in a Fusebox 4+ application?).

<cfdump var="#attributes#" label="Results" />

Your full code should look like this:

<cfset radioList = "one,two,three" />

<cfoutput>
<cfloop list="#radioList#" index="i">
<cfparam name="attributes.#i#" default="0" />
</cfloop>
</cfoutput>
<cfdump var="#attributes#" label="Results" />

Save the page with a .cfm extension and save in a test area of your development Web server. When you browse to the .cfm page you've created, the results should look something like this:


Now, if you add this line of code at the very beginning of your existing code:

<cfset attributes.one = 1 />

And browse to the page again, your results should look something like this:


CFPARAM just sets a default value for something, so in the first dump, all default values were assigned. In the second dump, we see that attributes.one already existed, and was assigned a value of "1". Because it already existed, the value was not overwritten.

Comments

Popular posts from this blog

Introduction to Artificial Intelligence - Microsoft - DAT263x on edX

Recently, my company encouraged us to learn more about Azure ML Studio.  A little Googling led me to a course on edx.org offered by Microsoft called Introduction to Artificial Intelligence .  While it was at least 50% advertisement for some of Microsoft's cloud-based AI offerings, it was definitely worth the 10 or so hours I put into it over the last 24 hours. This was a fairly high-level course that discussed aspects of statistical techniques like regression, categorization and clustering, as well as more advanced concepts including artificial intelligence (AI), natural language processing (NLP), and chatbots.  Overall, what I learned wasn't so much about these topics, but how much Microsoft's ML Studio has to offer in the way of getting you up and running quickly and easily with these technologies. Having said that, it's Microsoft, and there were several things that didn't work as they should have, as I expected.  Full disclosure:  I'm an AWS certified ...

Boston Housing Dataset Missing From UC Irvine's Site

I'm putting together a series of blog posts on Python for R programmers, and I figured I'd use the Boston dataset of Boston housing prices.  It's a pretty well-known dataset for regression, and it's included in R in the MASS package and in Python in sklearn.datasets .  I know the data originally came from UCI, so I wanted to give credit where credit was due. When I clicked the first few Google links that appeared, I got this message on the UCI site: I'm sorry, the dataset "housing" does not appear to exist. Weird that they're not linked at all, but I found these links. Here's the link to the dataset in their archive. Here's the link to the data dictionary. Enjoy!