View comments | RSS feed

cfloop: looping over a list or file

Description

Looping over a list steps through elements contained in any of these entities:

Syntax

<cfloop 
index = "index_name"
list = "list_items"
delimiters = "item_delimiter">
...
</cfloop>

See also

cfabort, cfbreak, cfexecute, cfexit, cfif, cflocation, cfswitch, cfthrow, cftry; cfloop and cfbreak in Elements of CFML in ColdFusion MX Developer's Guide

Attributes

Attribute Req/Opt Default Description

index

Required

 

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

list

Required

 

A list, variable, or filename; contains a list.

delimiters

Optional

 

Character(s) that separates items in list.

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>

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | KnowledgeBase | Bug Reporting

Version 7

Comments


No screen name said on May 21, 2005 at 4:04 AM :
You still haven't improved your documentation. Why do you keep saying this tag accepts a filename when it doesn't. As far as I can tell, filenames are treated as a list and the contents of the file are never read.
jrunrandy said on May 26, 2005 at 6:04 PM :
No screen name, I apologize for dropping the ball on your comment about the filename functionality. This functionality never existed but somehow it was added to the CFMX documentation.

I have added doc bug 60239 to help ensure that we fix it in the next release of the docs.
ACT Admin said on Oct 3, 2005 at 5:02 AM :
The code for looping over a file is also applicable to the text data from a textarea control, however the delimiter should be defined as #chr(13)##chr(10)# as this is the character order from a line return in the text.
ACT Admin said on Oct 3, 2005 at 5:38 AM :
(not that it affects cfloop greatly, but it becomes very relevant if you are processing blocks of text with a function like replace(), such as populating a text block with <br> tags, so it would be good practice to standardise the format)
vincentnofx said on Sep 17, 2008 at 3:13 PM :
Was this documentation written by carnies? Anyways, it looks like the cfloop tag won't work with the list= using an array of complex objects. This should be noted here. Also the index ISN'T the variable to receive the next list element. It's actually the numeric index into the list.
dlrider said on Oct 2, 2008 at 11:50 AM :
vincentnofx: "Also the index ISN'T the variable to receive the next list element. It's actually the numeric index into the list."

How so? It works for me the way it is stated: the next list element is placed into the index item. No numeric unless that is the data.

Ex.
<CFQUERY datasource="#APPLICATION.FTS_DS#">
<cfloop index = "ListElement" list="#form.cb_mail_list#">
insert into FTS_ENTRIES (GUID, Mail_List_ID)
values ('#MY_GUID#','#ListElement#')
</cfloop>
</CFQUERY>

form.cb_mail_list may contain something like "roofing,carpentry,flooring", and my_guid = xc314. The cfloop will make 3 entries (one for each) in the db table as 'xc314','roofing', 'xc314','carpentry' and 'xc314','flooring'.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/7/htmldocs/00000295.htm