Article

Home » Server-side Coding » ColdFusion Tutorials » Introduction to Data Structure in ColdFusion Part II – Structs

About the Author

David Medlock

author_davemedlock David operates MedlockWeb, a web consulting firm that focuses on building solid relationships and web applications with entrepreneurs and investors. He also maintains a web business blog at WebDevSuccess.com.

View all articles by David Medlock...

Introduction to Data Structure in ColdFusion Part II – Structs

By David Medlock

December 3rd, 2003

Reader Rating: 8.5

Page: 1 2 Next

Structures are a very useful means of organizing data in your ColdFusion programs. But, they're often misunderstood, overlooked, or not used to the full -- even by the most skilled ColdFusion coders.

In this article, we'll take a look at what structures are, and how you can use them. We'll also look at some of the most common structures that many people overlook on a regular basis.

What is a Struct?

First of all, what is a structure (or "struct', as it's known in ColdFusion)? A struct is a means of storing data. If you've done any programming in Java, you might liken it to a HashMap. It's also similar to a hash in Perl. We can store a series of related data in a struct. Every piece of data has a name and a value, and we can retrieve a particular value by calling the struct and passing it the name of the value we want. Think of this process as being similar to storing several variables with different names inside one variable.

Let's start with an example. Imagine we're building an online book store and we want to store information about a single book in one data structure. Now, normally, we would probably store our data in a database, and use a query to access and display it. But, for this example, we're going to use structs. Every book is going to have a title, an author, a description, a publish year, and an ISBN number. We'll start by creating a struct to hold this data.

<cfset myBook = StructNew()>

Here, we create a variable called myBook, and we tell ColdFusion that this is going to be a structure. If we don't explicitly tell ColdFusion that this will be a structure, it won't work correctly. Now, let's add our data to the struct:

<cfset a = StructInsert(myBook, \\"title\\", \\"All About ColdFusion\\", 1)>

Let's stop and talk about this StructInsert function before we move on. The first parameter of the function is the name of the struct to which we want to add a key. The next parameter is the name of the key we want to add. For this parameter, you can use a variable or a string literal (as we've done here). The next parameter is the value for the key. Just as with the name of the key, we can use a variable or a string literal in this case. The fourth parameter is an optional parameter -- the "allow overwrite" parameter. If this parameter is set to 1, it will overwrite any existing key named "title" in this struct. If it is set to 0, an error will be thrown if you try to add "title" to a struct that already has a key by that name. The default for this parameter is 0. So, let's add the rest of our data:

<cfset a = StructInsert(myBook, "author", "David Medlock", 1)>
<cfset a = StructInsert(myBook, "description", "Information about CF", 1)>
<cfset a = StructInsert(myBook, "publishyear", "2004", 1)>
<cfset a = StructInsert(myBook, "ISBN", "ABCD123456", 1)>

Ok, our book contains all the information we need. Now, we want to display our book. This is easy. If you know the names of all the keys for your structure, you can easily display them like this:

<cfoutput>
 Title: #myBook["title"]#<br />
 Author: #myBook["author"]#<br />
 Description: #myBook["description"]#<br />
 Publish Year: #myBook["publishyear"]#<br />
 ISBN: #myBook["ISBN"]#
</cfoutput>

Here, we simply dump out the contents of the structure, one key at a time. But, what happens if we don't know what keys a struct contains? It's no problem -- we can use ColdFusion's loop tag to output all the keys in the struct.

<cfloop collection="#myBook#" item="key">
 <cfoutput>
   #key#: #myBook[key]#<br />
 </cfoutput>
</cfloop>

That's really all we have to do in order to display all the keys in our structure and their values. But, as you'll notice, this doesn't output a very pretty label for each item. It just displays the key name. Let's work on fixing that next.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article