Looping Over a List

Description

Looping over a list lets you step through elements contained in a variable or value that is returned from an expression. In a list loop, the index attribute specifies the name of a variable to receive the next element of the list, and the list attribute holds a list or a variable that contains a list.

Category

Flow-control tags

Syntax

<cfloop index = "index_name"
  list = "list_items"
  delimiterS = "item_delimiter">
</cfloop> 

See also

cfabort, cfbreak, cfexecute, cfexit, cfif    cfelseif    cfelse, cflocation, cfswitch    cfcase    cfdefaultcase, cfthrow, cftry cfcatch

Attributes

Attribute
Description
index
Required. In a list loop, the index attribute specifies the name of a variable to receive the next element of the list, and the list attribute holds a list or a variable that contains a list.
list
Required. The list items in the loop, provided directly or in a variable.
delimiterS
Optional. Specifies the delimiter characters used to separate items in the list.

Example

This loop displays the names of each of the Beatles:

<cfloop index = "ListElement" 
  list = "John,Paul,George,Ringo"> 
    <cfoutput>#ListElement#</cfoutput><BR> 
</cfloop>

Although the default delimiter character is a comma, you can specify element boundaries in the delimiter attribute. Here's the same loop as before, but in this example cfloop treats commas, colons, or slashes as list element delimiters:

<cfloop index = "ListElement" 
  list = "John/Paul,George::Ringo" 
  delimiters = ",:/"> 
    <cfoutput>#ListElement#</cfoutput><BR> 
</cfloop>

Delimiters can be specified in any order. Consecutive delimiters are treated as a single delimiter; thus the two colons in the previous example are treated as a single delimiter between "George" and "Ringo."