Let's quickly review structures in ColdFusion so that useful comparisons with Java Collections are not lost. Despite their usefulness and their centrality to ColdFusion, developers often do not learn much about structures until late in their learning process.
Structures are used in ColdFusion to store data in name -value pairs. ColdFusion stores a lot of information internally as structures. All ColdFusion variables in the following scopes are stored as structures:
application
attributes
cfcatch
cferror
cffile
cgi
cookie
form
request
session
thistag
url
You can therefore reference variables in any of these scopes using dot notation or array (bracketed) syntax.
Structures are created like this:
<cfset stMyStruct = StructNew()>
or like this:
<cfscript> stMyStruct = StructNew(); </cfscript>
Once you have created a structure, you can populate it like so:
<cfset BaseballTeam = StructNew()> <cfset BaseballTeam.teamHome = "Arizona"> <cfset BaseballTeam.teamName = "Diamondbacks"> <cfset BaseballTeam.managerName = "Bob">
The properties of a structure are known as keys. In the above structure there are three keys: teamHome , teamName , and managerName . Each key has one value. You can then output the value of a specified key in three different ways:
<cfoutput> The name of the team is: #BaseballTeam.teamName# <br> The manager is: #BaseballTeam[managerName]# <br> This team lives in: #StructFind(BaseballTeam, teamHome)# </cfoutput>
Structures can hold other complex data types, including other structures. Structures are popular devices in ColdFusion, as they perform faster than using lists or arrays.
Just as you might use a structure in ColdFusion to store information about items in an online shopping cart, developers often will use one of the collections, such as a vector, for this purpose in a JavaServer Page.
Note
Even structures in ColdFusion are sometimes referred to as collections. You may recall that in ColdFusion there are several types of loops that one can employ using <cfloop> . The way to loop over a structure to output or manipulate its data is to specify the structure name using the collection attribute of the <cfloop> tag. Coincidence? Nope.
Let's turn now to structures to understand how to use them and how to choose among them.
Top |