|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
TOP COLDFUSION LINKS CF101 Structures and Arrays Part 2
Similar but different beasts
By: Jeffry Houser
Jun. 22, 2004 12:00 AM
Arrays and structures are complex data types in ColdFusion. It is important to have an understanding not only of how to use them, but also of when it is best to use one over the other. This is the second of a two-part series in which I teach you about structures and arrays. In Part 1 (ColdFusion Developer's Journal, Vol. 6, issue 5), I introduced structures and arrays, and investigated arrays in more detail. In this article I'll go into greater detail on structures and then compare and contrast the two complex data types. Reviewing Structures and Arrays If you are familiar with other programming languages, you may have heard of something called associative arrays. Structures in ColdFusion are similar to associative arrays in other languages. The index is a name that is associated with the value you place in it. The elements that make up a structure are known as key/value pairs. The key is another name for the index and, as I said, is used to access the value. The value portion of a structure can be any valid value in ColdFusion, including integers, strings, arrays, objects, or even other structures. It might help you to conceptualize a structure as a two-column spreadsheet, with the key being one column and the value being the second column. Structures are great for storing related elements. In one of my current projects, we are using a structure to store directory information about the project, such as the location of include files, image files, custom tags, and ColdFusion Components. The elements of this structure might look like Table 1. Throughout our application, it is easy to access the directory information we need just by grabbing the proper values out of the structure. Creating and Using Structures <cfset MyStruct = StructNew()> Unlike ArrayNew(), the StructNew function does not accept any arguments. Creating a structure is easy. The next step is to learn how to add elements to the structure. To understand that, you must first know how to access elements of a structure. There are two methods for accessing elements of a structure: dot notation and associative array notation (also called square-bracket notation). Dot notation takes this form: Structure.Key It starts with the name of the structure, following that is a period, and after the period comes the key. I've also heard dot notation referred to as object property notation because the syntax is very similar to the way you would access properties of an object. This is the most common syntax used for accessing structure keys because it is very similar to the way you may access variables in a variable scope or columns in a query result set. To add an element into a structure using dot notation we can use the cfset tag, like this: <cfset MyStruct.Includes = "inc/"> The line of code starts with the cfset tag. It is followed by a space and then the name of the structure. The structure is followed by a dot and then the name of the key. After that we have an equals sign and the value. This line of code creates the key, Includes, inside the structure, MyStruct, and gives it a string value of inc/. Associative array notation is another way to access elements of a structure. You may remember how we accessed values in an array in Part 1. We used the name of the array, then an open square bracket "[", followed by the index number, and finished off with a close square bracket "]". To access a structure's value using associative array notation, we use the same approach. The only difference is that instead of placing a number between the brackets, we use a string. Here is an example: <cfset MyStruct["Images"] = "Images/"> This line of code creates the key, Images, inside the structure, MyStruct, and gives it the string value of Images/. There are quite a few benefits to using the associative array notation over the dot notation. First, your key name can have spaces or special characters. Second, you don't have to know the name of the key when setting or accessing it. The key name can be a variable or an unknown value. Dynamically named structures can be great if you don't know what you are going to be storing beforehand. A third benefit of this method is that the keys will retain their case. Using the object property notation, all keys will be converted to uppercase. Next, we'll look at an example of using array notation to dynamically name a structure. Dynamically Named Structures <form action="loginip.cfm" method="post"> The form accepts two values, a username and a password, and has a submit button. The code on the form-processing page (loginip.cfm) that will store the username and time stamp in the application scope looks like this: <cfset application.Users[form.username] = Now()> Under normal circumstances you'll have more code to process the login, but this line is fine for demonstration purposes. The line of code adds a new element to the users structure in the application scope. The name is the value of username, which the user entered in the login form. The value is gotten by using one of ColdFusion's date functions, now(), which returns the server's current datetime stamp. Note that before using the application scope you'll have to set it up with the cfapplication tag. Although it is beyond the scope of this article, more information on using the cfapplication tag can be found at Click Here !. Because the "form.username" text is not inside quotes, ColdFusion recognizes it as a variable and uses the value rather than a string literal. The line of code shown earlier could also be rewritten like this: <cfset application.Users["#form.username#"] = Now()> It would work just as well. ColdFusion recognizes the pound signs inside the string and knows to evaluate the string. I prefer the first method because it is easier to code and easier to read. However, the second method is the one of choice if you are mixing a string literal with a variable's value to form the variable name, like this: <cfset application.Users["Users#form.username#"] = Now()> To help with the processing of structures, we can use the cfloop tag to loop over structures. The cfloop tag accepts two attributes for looping over a structure, the collection attribute and the item attribute. The collection attribute accepts the structure value. The item attribute specifies the name of a temporary variable. The variable can be used inside the loop to access the specific element of the structure. The following code snippet is an example of how to loop over the users structure: <cfloop collection="#application.users#" item="Temp"> The snippet starts with the cfloop open tag. For the collection attribute, we specify the name of our structure, application.users. You'll notice that we specify the pound signs around it. The item attribute we specify is temp, a temporary variable. We are going to display each key and its value inside the loop. First we display the key, which is stored in the variable temp. We use the dynamic addressing and the associative array notation to get the value of the structure. It is worth nothing that when you create a structure in ColdFusion MX, you do not have to use the StructNew function. Using a cfset tag with either type of notation will create a structure on the fly. ColdFusion will explicitly create a structure without using the StructNew function. I generally avoid this practice for two reasons. First, using StructNew to explicitly define the structure will make the code more readable when it has to be revisited in six months. The second reason is backward compatibility. Versions earlier than ColdFusion 5 do not support the creation of structures without the use of the StructNew function. However, pre-CFMX versions do support the use of periods inside a variable name, so your code will work. The difference between a structure and a variable name with a period in it is an important distinction. Structure Functions
Conclusion CFDJ LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||