Adobe ColdFusion 8

Using for-in loops

The for-in loop loops over the elements in a ColdFusion structure. It has the following format:

for (variable in structure) statement

The variable can be any ColdFusion identifier; it holds each structure key name as ColdFusion loops through the structure. The structure must be the name of an existing ColdFusion structure. The statement can be a single semicolon terminated statement or a statement block in curly braces.

The following example creates a structure with three elements. It then loops through the structure and displays the name and value of each key. Although the curly braces are not required here, they make it easier to determine the contents of the relatively long WriteOutput function. In general, you can make structured control flow, especially loops, clearer by using curly braces.

myStruct=StructNew();
myStruct.productName="kumquat";
mystruct.quality="fine";
myStruct.quantity=25;
for (keyName in myStruct) {
    WriteOutput("myStruct." & Keyname & " has the value: " &
            myStruct[keyName] &"<br>");
}

Note: Unlike the cfloop tag, CFScript for-in loops do not provide built-in support for looping over queries and lists.