Not yet rated

Problem

You need to loop over a structure and output the values.

Solution

Outputting values from a structure might seem a little difficult at first. Unlike arrays, which are index based, structures are key based. But, as it does with just about everything else, ColdFusion provides a simple solution!

Detailed explanation

 

Using a Collection Loop:

In order to loop over a structure you will need to use a collection loop.  A collection loop is similar to an array loop, only you provide the collection and item attributes rather than the array and index attributes.

<!--- create the structure --->

<cfset superBowlChamps = {
TEAM = "Indianapolis Colts",
QB = "Peyton Manning",
WR = "Reggie Wayne",
RB = "Joseph Addai",
COACH = "Jim Caldwell"
} />

<!--- Loop over the structure and output the key|value pairs
--->

<cfoutput>
<ul>
<cfloop collection="#superBowlChamps#" item="i">
<li>#i#: #superBowlChamps[i]# </li>
</cfloop>
</ul>
</cfoutput>

 

 


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes