cfloop: looping over an array, list, file, or struct

Description

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

  • A variable
  • A value that is returned from an expression
  • An array
  • A file Looping over a file does not open the entire file in memory.

Syntax

<cfloop 
index = "index name"
item = "item name"
array = "array" 
characters = "number of characters" 
delimiters = "item delimiter" 
file = "absolute path and filename"
charset = "a charset to use when reading the file" 
list = "list items" 
... 
</cfloop>

See also

cfabortcfbreakcfcontinuecfexecutecfexitcfifcflocationcfswitchcfthrowcftrycfloop and cfbreak in the Developing ColdFusion Applications

History

ColdFusion (2018 release) Update 2: The script variant of cfloop supports iterating over an array, list, and struct.

ColdFusion 8: Added the characters, file, and array attributes.

cfloop - Array

Attribute

Required/Optional

Description

array

Required

The array object.

index

Optional. In earlier versions of ColdFusion, this attribute was required. In ColdFusion (2016 release), this attribute is optional. Either item or index is required. 

The current value of the array.

item

Optional

The current element of the array. In ColdFusion (2016 release), you need to use either item or index; or use both the attributes together.

The following code sample throws an exception, since neither item nor index was used:

<cfset myArray = ["John", "Paul", "George", "Ringo"] >
<cfloop array="#myArray#">
   <cfoutput>#x# <br/> </cfoutput>
</cfloop>

Use the attribute index as shown below:

 

<cfset myArray = ["John", "Paul", "George", "Ringo"] >
<cfloop array="#myArray#" index="idx">
   <cfoutput>#idx# <br/> </cfoutput>
</cfloop>

Output

John Paul George Ringo

Use the attribute item, as shown below:

 

<cfset myArray = ["John", "Paul", "George", "Ringo"] >
<cfloop array="#myArray#" item="itm">
   <cfoutput>#itm# <br/> </cfoutput>
</cfloop>

Output

John Paul George Ringo

Use both item and index attributes together, as shown below:

<cfset myArray = ["John", "Paul", "George", "Ringo"] >
<cfloop array="#myArray#" item="Beatles" index="name">
   <cfoutput>#name# : #Beatles# <br/> </cfoutput>
</cfloop>

Output

1 : John 2 : Paul 3 : George 4 : Ringo

 

Looping over multi-dimensional arrays

A multi-dimensional array is an array of arrays rather than an array of single values.

To read an element from a two-dimensional array, you must first identify the index of the row and then identify the index of the column. For example, the song "Surfer Girl" is in row 3, column 1, so it is identified as aBeachBoysAlbums[3][1].

To loop through a two-dimensional array, you must nest one loop inside of another.

<cfset aBeachBoysAlbums = ArrayNew(2)>

<!---Build first album array, Surfin' Safari--->
<cfset aBeachBoysAlbums[1][1] = "Surfin' Safari">
<cfset aBeachBoysAlbums[1][2] = "County Fair">
<cfset aBeachBoysAlbums[1][3] = "Ten Little Indians">

<!---Build second album array, Surfin' USA--->
<cfset aBeachBoysAlbums[2][1] = "Surfin' USA">
<cfset aBeachBoysAlbums[2][2] = "Farmer's Daughter">
<cfset aBeachBoysAlbums[2][3] = "Miserlou">

<!---Build third album array, Surfer Girl--->
<cfset aBeachBoysAlbums[3][1] = "Surfer Girl">
<cfset aBeachBoysAlbums[3][2] = "Catch a Wave">
<cfset aBeachBoysAlbums[3][3] = "The Surfer Moon">

<!---<cfdump var="#aBeachBoysAlbums#" >--->


<!--- loop --->

<cfloop from="1" to="#ArrayLen(aBeachBoysAlbums)#" index="i">
 <cfoutput>
  <cfloop from="1" to="#ArrayLen(aBeachBoysAlbums[i])#" index="j">
   Album: #i# Song #j#: #aBeachBoysAlbums[i][j]#</br>
  </cfloop>
 </cfoutput>
</cfloop>

Output

Album: 1 Song 1: Surfin' Safari
Album: 1 Song 2: County Fair
Album: 1 Song 3: Ten Little Indians
Album: 2 Song 1: Surfin' USA
Album: 2 Song 2: Farmer's Daughter
Album: 2 Song 3: Miserlou
Album: 3 Song 1: Surfer Girl
Album: 3 Song 2: Catch a Wave
Album: 3 Song 3: The Surfer Moon

 

Alternatively, you can use an array of structures and loop through the array to extract the key-value pairs.

For example,

<cfscript>
data = [
    {code:"USA", count:324459463},
    {code:"UK", count:66181585},
    {code:"CAN", count:36624199},
    {code:"FRA", count:64979548},
    {code:"NED", count:17035938},
    {code:"GER", count:82114224}
];
</cfscript>

<cfloop array="#data#" index="i">
   <cfoutput>
    The population of #i.code#: #i.count#<br>
   </cfoutput>
</cfloop>

Output

The population of USA: 324459463
The population of UK: 66181585
The population of CAN: 36624199
The population of FRA: 64979548
The population of NED: 17035938
The population of GER: 82114224

 

cfloop as script

 

<cfscript>
    myArray = ["John", "Paul", "George", "Ringo"];
    cfloop(array=myArray, index="idx"){
        writeOutput(#idx# & "<br/>");
    }
</cfscript>

Output

John
Paul
George
Ringo

cfloop - List

Attribute

Required/Optional

Description

list

Required

List of elements.

index

Optional

The current value of the list.

item

Optional

The current element of the list. In ColdFusion (2016 release), you need to use either item or index; or use both the attributes together.

Delimiters

Optional

Characters that separate list items.

The following code sample throws an exception, since neither item nor index was used:

<cfset myList="John,Paul|George|Ringo"/>
<cfloop list="#myList#" delimiters=",|">
 <cfoutput>
  #name#
 </cfoutput>
</cfloop>

Use the attribute index as shown below. The sample below does not use delimiters attribute.

<cfset myList="John,Paul,George,Ringo"/>
<!---Loop through the list with attribute index --->
<cfloop list="#myList#" index="name">
 <cfoutput>
  #name#
 </cfoutput>
</cfloop>

Output

John Paul George Ringo

Use the attribute item as shown below. The sample below does not use delimiters attribute.

 

<cfset myList="John,Paul,George,Ringo"/>
<!---Loop through the list with attribute item --->
<cfloop list="#myList#" item="Beatles">
 <cfoutput>
  #Beatles#
 </cfoutput>
</cfloop>

Output

John Paul George Ringo

You can use both item and index together, as shown below:

 

<cfset beatles="John,Paul,George,Ringo"/>
<!---Loop through the list with attributes item and index --->
<cfloop list="#beatles#" item="name" index="i">
 <cfoutput>
  #i#:#name#
 </cfoutput>
</cfloop>

 

Using delimiters in cfloop - list

Delimiters are characters that separate list items. For example,

  • Item1, item2, item3, item4 (comma-separated list)
  • Item1, item2 | item3 | item4 (comma and pipe-separated list)

In the code samples below:

<cfset myList="John,Paul|George|Ringo"/>
<!---Loop through the list with attributes index and delimiters --->
<cfloop list="#myList#" index="name" delimiters="|">
 <cfoutput>
  #name#
 </cfoutput>
</cfloop>

 

Output

John, Paul George Ringo

<cfset myList="John,Paul|George|Ringo"/>
<!---Loop through the list with attributes index and delimiters --->
<cfloop list="#myList#" index="name" delimiters=",|">
 <cfoutput>
  #name#
 </cfoutput>
</cfloop>

Output

John Paul George Ringo

 

cfloop as script

 

<cfscript>
    myList="1,2,3,4"
    cfloop(list=myList, index="i", item="j") {
        writeOutput("index:" & i)
        writeOutput("item:" & j & "")
    }
</cfscript>

Output

index:1item:1
index:2item:2
index:3item:3
index:4item:4

cfloop - File

Attribute

Required/Optional

Description

file

Required

The file attribute denotes the absolute path and filename of the text file to read, one line at a time. When reading large text files, you can reuse the value of the index variable, which contains the current line of the file. When the loop completes, ColdFusion closes the file.

item

Optional

Values are line or chars (In ColdFusion 2016). Denotes how the file is read.

index

Optional

In ColdFusion (2016 release), you need to use either item or index; or use both the attributes together.

character

Optional

The attribute denotes the number of characters to read during each iteration of the loop from the file specified in the file attribute. If the value of the characters attribute is more than the number of characters in the file, ColdFusion uses the number of characters in the file.

charset

Optional

Character set to use when reading the file.

In the code sample below, ColdFusion opens the text file, myfile.txt, reads each character, and displays the first 20 characters (including spaces) during each iteration of the loop.

<cfset myFile="c:\out\myfile.txt"/>
<cfloop file="#myFile#" index="i" item="chars" characters="20">
    <cfoutput>
        #i#:#chars#<br />
    </cfoutput>
</cfloop>
cfloop- file
cfloop- file

The next sample reads each line of the file, using the index and item attributes.

<cfset myFile="c:\out\myfile.txt"/>
<cfloop file="#myFile#" index="i" item="line">
    <cfoutput>
        #i#:#line#<br />
    </cfoutput>
</cfloop>
cfloop- file using index and item
cfloop- file using index and item

cfloop - Struct

When looping in a struct, each item in the struct is referenced by the variable name in the item attribute. The loop executes until all items have been accessed.

Attribute

Required/Optional

Description

collection

Required

A struct object.

item

Required

The structure key that is used to iterate through the key-value pairs. The collection attribute is used with the item attribute.

For example, in the sample below, the item key iterates through each struct item and displays the key-value pair.

<cfscript>
 Team = {"Marketing" = "John", "Sales" : {"Executive" : "Tom", "Assistant" = "Mike"},"IT":{"Developers":{"Dev1":"Ashley","Dev2"="Jason"}}};
</cfscript>
<cfloop collection="#Team#" item="key" >
        <cfoutput>#Key#:</cfoutput>
  <cfdump var="#Team[key]#">
        <br/>
</cfloop>
cfloop- struct
cfloop- struct

 

cfloop as script

 

<cfscript>
   Team = {"Marketing" = "John", "Sales" : {"Executive" : "Tom", "Assistant" = "Mike"},"IT":{"Developers":{"Dev1":"Ashley","Dev2"="Jason"}}};
   cfloop(collection=Team ,item="key"){
        writeOutput(#Key# & ":");
        writeOutput(#SerializeJSon(Team[key])#);
        writeOutput("<br/>");
    }
</cfscript>

Output

Marketing:"John"
IT:{"Developers":{"Dev2":"Jason","Dev1":"Ashley"}}
Sales:{"Executive":"Tom","Assistant":"Mike"}

 Adobe

Pridobite pomoč hitreje in preprosteje

Ali ste nov uporabnik?

Adobe MAX 2024

Adobe MAX

The Creativity Conference

14.–16. oktober v Miami Beachu in spletu

Adobe MAX 2024

Adobe MAX

The Creativity Conference

14.–16. oktober v Miami Beachu in spletu