ColdFusion 9.0 Resources |
cfloop: looping over a list, a file, or an arrayDescriptionLooping over a list steps through elements contained in any of these entities:
Syntax<cfloop index = "index name" array = "array" characters = "number of characters" delimiters = "item delimiter" file = "absolute path and filename"> list = "list items" ... </cfloop> See alsocfabort, cfbreak, cfcontinue, cfexecute, cfexit, cfif, cflocation, cfswitch, cfthrow, cftry; cfloop and cfbreak in the Developing ColdFusion Applications Attributes
ExampleThis 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 as follows: <cfloop file="c:\temp\simplefile.txt" index="line"> <cfoutput>#line#</cfoutput><br> </cfloop> To read a specified number of characters from a text file during each iteration of the loop, use the tag as follows: <cfloop file="c:\temp\simplefile.txt" index="chars" characters="12"> <cfoutput>#chars#</cfoutput><br> </cfloop> When you read the following text file, ColdFusion reads 12 characters during each iteration of the loop; the result appears as follows:
To loop over an array, you can do the following: <cfset x = ["mars","earth", "venus", "jupiter"]> <cfloop array="#x#" index="name"> <cfoutput>#name#</cfoutput><br> </cfloop> |