CFLOOP: LOOPING OVER A LIST OR FILE  
Description
Looping over a list steps through elements contained in any of these entities:
  • A variable
  • A value that is returned from an expression
  • A file
 
Syntax
<cfloop 
   index = "index_name"
   list = "list_items"
   delimiters = "item_delimiter">
   ...
</cfloop>
 
See also
cfabort, cfbreak, cfexecute, cfexit, cfif, cflocation, cfswitch, cfthrow, cftry
 
Example
This loop displays four names:
<cfloop index = "ListElement" 
   list = "John,Paul,George,Ringo"> 
      <cfoutput>#ListElement#</cfoutput><br> 
</cfloop>
You can put more than one character in the delimiters attribute, in any order. For example, this loop processes commas, colons, and slashes as list delimiters:
<cfloop index = "ListElement" 
   list = "John/Paul,George::Ringo" 
   delimiters = ",:/"> 
      <cfoutput>#ListElement#</cfoutput><br> 
</cfloop>
ColdFusion skips the second and subsequent consecutive delimiters between list elements. Thus, in the example, the two colons between "George" and "Ringo" are processed as one delimiter.
To loop over each line of a file, use the tag this way:
<cfloop list="#theFile#" 
   index="curLine" 
   delimiters="#chr(10)##chr(13)#"> 
   ...
</cfloop>
 
cfloop: looping over a COM collection or structure
Description
The cfloop collection attribute loops over every object within a COM/DCOM collection object, or every element in a structure:
  • A COM/DCOM collection object is a set of similar items referenced as a group. For example, the group of open documents in an application is a collection.
  • A structure contains a related set of items, or it can be used as an associative array. Looping is particularly useful when using a structure as an associative array.
In the loop, each item is referenced by the variable name in the item attribute. The loop executes until all items have been accessed.
The collection attribute is used with the item attribute. In the example that follows, item is assigned a variable called file2, so that with each cycle in the cfloop, each item in the collection is referenced. In the cfoutput section, the name property of the file2 item is referenced for display.
 
Example
This example uses a COM object to output a list of files. In this example, FFunc is a collection of file2 objects.
<cfobject 
   class = FileFunctions.files 
   name = FFunc 
   action = Create> 
<cfset FFunc.Path = "c:\"> 
<cfset FFunc.Mask = "*.*" > 
<cfset FFunc.attributes = 16 > 
<cfset x = FFunc.GetFileList()> 
<cfloop collection = #FFUNC# item = "file2"> 
   <cfoutput>      #file2.name# <br>    </cfoutput> 
</cfloop>
<!---Loop through a structure that is used as an associative array: --->
...<!--- Create a structure and loop through its contents --->
<cfset Departments = StructNew()>
<cfset val = StructInsert(Departments, "John ", "Sales ")>
<cfset val = StructInsert(Departments, "Tom ", "Finance ")>
<cfset val = StructInsert(Departments, "Mike ", "Education ")>
<!--- Build a table to display the contents --->
<cfoutput>
<table cellpadding = "2 " cellspacing = "2 ">
   <tr>
      <td><b>Employee</b></td>
      <td><b>Dept.</b></td>
   </tr>
<!--- Use item to create the variable person to hold value of key as loop runs 
--->
<cfloop collection = #Departments# item = "person ">
   <tr>
      <td>#person#</td>
      <td>#StructFind(Departments, person)#</td>
   </tr>
</cfloop>
</table>
</cfoutput>
INDEX  
  Required
 

In a list loop, the variable to receive next list element.

LIST  
  Required
 

A list, variable, or filename; contains a list

DELIMITERS  
  Optional
 

Character(s) that separates items in list