This is the fifth installment of a 6 part series. All sections will help to introduce you to some basic programming concepts that will all carry through and help you as you learn ColdFusion. If you are looking for any of the other installments to this series, you can get to them below.
- What is Programming?
- Proper Syntax
- Variables and Data Types
- Conditional Processing
- Arrays and Structures
- Modular Code
Arrays and Structures
Now the fun begins! Arrays and structures are complex variables that store data in a way that you can easily and efficiently use them. When I was first learning ColdFusion I will admit, I had a hard time wrapping my head around the concept of exactly what an array/structure was and I was very confused on how to write the code for each. I had never worked with data outside of a database so this was a pretty foreign concept to me. Thankfully, you won’t have to struggle like I did because I’m about to lay it out in a way I wish someone had done for me all those years ago.
Let’s start with structures because I think they are a bit easier to understand and to code. If you remember when we first talked about variables, I showed you how easily we could store data in it such as our “thishat” variable and the value “red”. But what if I wanted to collect more details about your hat such as the size, material and brand. Without a structure, I could store them all individually like so:
<cfset hatcolor = "red"> <cfset hatsize = "Large"> <cfset material = "Wool"> <cfset brand = "No Fear">
That works, sure, but it’s a lot of management to keep up with that and you can’t do much with it as far as data manipulation. Instead, I will create a new structure for “thishat”. This doesn’t do anything other than just say “hey, I’m about to make a structure so make a variable to hold it”.
<cfset thishat = structnew()>
Next, I’ll add the separate properties of the structure. This basically tells the variable “thishat” that I’m adding some properties to it.
<cfset thishat.hatcolor = "red"> <cfset thishat.hatsize = "Large"> <cfset thishat.material = "Wool"> <cfset thishat.brand = "No Fear">
Now anytime I want to reference an item in the structure I can simply output like I would any other variable. For example, if I wanted to display the material property:
You might be wondering why not just set up individual variables like I did in the first example. Well, there are things you can do with a structure that you can’t do with just a simple variable. For example, because a structure is more like a table or recordset, you can sort it. You can also return a list of the properties, known as keys, that exist in the structure. You can also search and clear structures as well as determine if a variable is in fact a structure.
Structures are very versatile and are a great method for storing related together into a single complex variable.
Now that you’ve learned about structures it’s time to move onto a more advanced data control called arrays. Arrays are a lot like structure in that they store data in a single variable. However, there are two major differences. First, while a structure simply uses the structure name and property dot delimited syntax (thishat.material), an array is referenced numerically. Second, structures are single dimensional and arrays can be multidimensional. Although you can create a single dimensional array as well.
What the heck does that mean? Let’s break it down so it’s easy to understand. Structures can hold all those details we wanted to store in a single dimensional array, but that means we can only store it for one hat. But if they were in an array, which is multidimensional, we could store those same details, but instead of just 1 hat, we could store it for 10 or 20 or 30 hats and simply reference them by number.
Let’s look at some code examples and see if we can put it together. We will start with a single dimensional array.
Above, I’ve simply created the variable and made a one dimensional array. Next I can add items to the array.
<cfset ArrayAppend(hatcolor, "Red")> <cfset ArrayAppend(hatcolor, "Blue")> <cfset ArrayAppend(hatcolor, "Green")>
Of course, ColdFusion is very flexible in it’s coding syntax and there is actually an even shorter way of writing the above:
<cfset hatcolor = ["Red", "Blue", "Green"]>
In the above, I’ve created the array and added values all in one step. Time saver! In both examples, I’ve created an array that visually looks like this:
And if I wanted to display on screen the third element in the list, which is Green, I would reference the array and the item in the array that corresponds to that number like so:
Now, this example could just as easily be done using a regular single structure so let’s instead move onto a 2 dimensional array so you can really see how arrays can start look even more like a table. In this example, I want to store several values for multiple hats. Something that would look like this:
I’m going to use the shorter method to create the array and just use the following syntax:
<cfset Allhats = [["Red", "Blue", "Green"],["Large", "Small", "Medium"],["Wool", "Cotton", "Leather"]]>
If I were to display this array on your screen, it would look like this:
What’s missing that you had in structures? Variable names! Arrays do not reference values by using their name. Taking a look at the ColdFusion dump using <cfdump> that shows us nothing other than values. We don’t know what these value represent. This is part of the problem and why I personally will choose a structure over an array if it’s a single dimensional array.
Now that you’ve read the basics of structures and arrays and understand the concept behind them (don’t worry, the code doesn’t matter at this point) I can now blow your mind and tell you it can get even more confusing (thankfully that’s not part of this tutorial)! Remember when I said structures are one dimensional? Well, there are more advanced techniques we will talk about elsewhere on this blog where we can actually store a structure inside of a structure. But if you’ve made it this far you’ve definitely got the basics down. So let’s cover our last section of this series, Modular Code.