Looping over a COM Collection or Structure  
 
 

The CFLOOP COLLECTION attribute allows you to loop over a structure or a COM/DCOM collection object:

  • A COM/DCOM collection object is a set of similar items referenced as a group rather than individually. For example, the group of open documents in an application is a type of collection.
  • A structure can contain either a related set of items or be used as an associative array. Looping is particularly useful when using a structure as an associative array.

Each collection item is referenced in the CFLOOP by the variable name you supply in the ITEM attribute. This type of an iteration is generally used to access every object within a COM/DCOM collection or every element in the structure. The loop is executed until all objects have been accessed.

The COLLECTION attribute is used with the ITEM attribute in a CFLOOP. In the example that follows, ITEM is assigned a variable called file2, so that with each cycle in the CFLOOP, each item in the collection is referenced. In the CFOUTPUT section, the name property of the file2 item is referenced for display.

 
 
  Examples  
 

This example employs a COM object to output a list of files. In this example, FFUNC is a collection of file2 objects.

<CFOBJECT CLASS=FileFunctions.files 
    NAME=FFunc 
    ACTION=Create> 
 
<CFSET FFunc.Path = "c:\"> 
<CFSET FFunc.Mask = "*.*" > 
<CFSET FFunc.attributes = 16 > 
<CFSET x=FFunc.GetFileList()> 
 
<CFLOOP COLLECTION=#FFUNC# ITEM=file2> 
    <CFOUTPUT> 
        #file2.name# <BR> 
    </CFOUTPUT>  
</CFLOOP>

This example loops through a structure (used as an associative array):

...<!--- Create a structure and loop through its contents --->
<CFSET Departments=StructNew()>
<CFSET val=StructInsert(Departments, "John", "Sales")>
<CFSET val=StructInsert(Departments, "Tom", "Finance")>
<CFSET val=StructInsert(Departments, "Mike", "Education")>

<!--- Build a table to display the contents --->

<CFOUTPUT>
<TABLE cellpadding="2" cellspacing="2">
    <TR>
    <TD><B>Employee</B></TD>
    <TD><B>Dept.</B></TD>
    </TR>

<!--- In CFLOOP, use ITEM to create a variable 
    called person to hold value of key as loop runs --->
<CFLOOP COLLECTION=#Departments# ITEM="person">
    <TR>
    <TD>#person#</TD>
    <TD>#StructFind(Departments, person)#</TD>
    </TR>
</CFLOOP>
</TABLE>
</CFOUTPUT>
...



 
 
BackUp LevelNext
 
 

allaire     AllaireDoc@allaire.com
    Copyright © 1998, Allaire Corporation. All rights reserved.