View comments | RSS feed
Contents > CFML Reference > ColdFusion Tags > cfloop: looping over a list or file PreviousNext

cfloop: looping over a list or file

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

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

cfabort, cfbreak, cfexecute, cfexit, cfif, cflocation, cfswitch, cfthrow, cftry

Attribute

Req/Opt

Default

Description

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

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>

Contents > CFML Reference > ColdFusion Tags > cfloop: looping over a list or file PreviousNext

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

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


merlinox said on May 31, 2004 at 3:17 AM :
Aren't a counter (like currentRow) for this tag?
Homesite tag - insight show me also startrow parameter for CFLOOP but It doesn't work.
halL said on Jun 1, 2004 at 7:57 AM :
The cfloop tag documentation covers several HTML pages, and each page describes a different kind of loop.
The page you are viewing documents looping over a ColdFusion list (or a file that contains a list), but not over a query.
For the syntax of the loop over a query see http://livedocs.macromedia.com/coldfusion/6.1/htmldocs//tags-p79.htm. (You can get to the page by clicking the left arrow at the top or bottom of this page.)

That said, here is a little more information:
The startrow (and endrow) attributes are used in the query loops only, not for list loops.
To determine the current query row inside any loop over a query (whether using cfloop or cfoutput), use the queryname.currentrow variable.
No screen name said on Oct 16, 2004 at 4:53 PM :
When I try looping over a file as in the example, my index param seems to only contain the filename I specified and the loop only executes once.
e.g. this code:

<cfset srcFile = "C:\Inetpub\wwwroot\Test\mail2.log">
<cfloop list="#srcFile#" index="curLine" delimiters="#Chr(10)##Chr(13)#">
<cfoutput>#curLine#</cfoutput><br>
</cfloop>

outputs only a single line:

C:\Inetpub\wwwroot\Test\mail2.log

The file contains 3 words separated by CRLF's. What am I doing wrong?
LoveErrors said on Nov 11, 2004 at 10:16 AM :
when will cfloop have a cfcontinue or cfnext attribute for cfloop? kind of amazed how you left something so BASIC to looping out...well, not really.
No screen name said on Nov 26, 2004 at 7:34 PM :
Apparently Macromedia staff has stopped monitoring LiveDocs ... ? Waiting since 10/16 for an answer to my question.
jrunrandy said on Nov 27, 2004 at 1:34 PM :
No Screen Name,
I'm sorry, but LiveDocs doesn't work like that. It isn't support for your coding questions. It doesn't receive enough traffic to handle questions like this effectively. I recommend that you post your issue to the online forums: http://webforums.macromedia.com/coldfusion/
No screen name said on May 21, 2005 at 3:54 AM :
jrunrandy, your comment is inapropriate. The question was relevant to the (poor) documentation provided. In the time it took you to berate the user you could have answered the question and saved me the trouble of looking elsewhere for an answer to why looping over a file doesn't work as expected.
jrunrandy said on May 26, 2005 at 6:03 PM :
No screen name and 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.
No screen name said on Jul 25, 2005 at 7:03 PM :
The function works as described if the contents of var "theFile" is not the name of the file, but the returned value of a cffile action="read" operation.

Thus:
<cffile action="read"
file="D:\\Inetpub\\wwwroot\\whatever.txt"
variable="theFile"
>

<pre>
<cfloop list="#theFile#"
index="curLine"
delimiters="#chr(10)##chr(13)#">
<cfoutput>Line of file: #curLine#</cfoutput>
</cfloop>
</pre>
heashon2000 said on Mar 15, 2006 at 12:54 PM :
This is a response to merlinox's question about a 'currentrow' variable when loopin over a list. You can do this yourself by setting a counter inside the loop itself:

<cfloop list="1,2,3" index="i">
<cfscript>
if(IsDefined("vCounter")){
vCounter = IncrementValue(vCounter);
}else{
vCounter = 1;
}
</cfscript>

<!--- put the rest of your looping code here. --->
</cfloop>

 

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

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/tags-p80.htm