Understanding and Using Structures and Associative Arrays ColdFusion Help

ColdFusion offers another complex data structure: structures. Some developers, including those familiar with JavaScript, will know structures as associative arrays. Where standard arrays were used to create a numerically indexed group of elements, associative arrays create key-value pairs. That is, they associate one value with another where neither the key nor the value needs to be an arbitrary numeric value. This type of association is also similar to the properties of objects in object-oriented and object-based programming languages such as Java, C++, and JavaScript

In fact ColdFusion offers three ways to refer to elements in a structure:

Using ColdFusion functions
• Usirig associative-array style notation
• Using object-property style notation

In this section you will consider all three ways to work with structures but first you will take a quick look at exactly how key-value pairs work and can be used.

Understanding the Concept of Key-Value Pairs

In an associative array each entry in the array has as its index a value that is usually a string or number and then takes as its value any valid ColdFusion data type.

For instance consider an array that you want to use to associate Social Security numbers with people’s names. This would be difficult with regular arrays because they take only numeric indexes and the indexes must be sequential starting at one.

But with a structure you can directly associate the Social Security numbers with the names, creating a data structure that performs mappings like those in Table 14.5.

TAB l E 1 4 . 5:

TAB l E 1 4 . 5:

 

This ability to associate two value.. an be used in several ways. For instance you could manipulate employee records for a company by using a structure to track a single employee: entries in the structure might include last name first name gender salary employee identification number Social Security number and more

Using Strudure Fundions

By now most of the functions for manipulating structures will perform familiar tasks because of their similarities to functions you have already encountered for manipulating lists and arrays

ColdFusion provides the functions outlined in Table 14.6 for working with structures.

TAB l E 1 4 . 6

TAB l E 1 4 . 6

TAB LE 14 . 6:

TAB LE 14 . 6:

 

For most of these functions their purpose and use is fairly straightforward. However . the meaning of a few arguments needs further discussion

The Allow<>verWrite argument of StructInsert is an optional argument that govems the behavior of the function when it tries to insert a key-value pair where the key already exists in the structure. If A11ov.<>verWrite is True, then the key’s value is replaced; otherwise it isn’t and an error occurs. By default, A11ov.<>verWrite is assumed to be False

For example, if you want to insert the key-value pair “123-45-6789′ ,”Joe Smith” and allow overwriting of existing values in the structure, you simply use Structlnsert- (StructureName, “123-45-6789”, “Joe Smith”,True).

The StructDelete function has an optional argument, Indi cateNotExisting. The purpose of this argument determines the behavior of the function when the key specified. for deletion does not exist. If IndicateNotExi sti ng is True, then the function ignores the fact that the specified key doesn’t exist and causes StructDe 1ete to return True, indicating successful completion of the deletion

By contrast if the value is False and the specified key doesn’t exist, then the function fails and StructDelete returns False.

Putting It All Together

To better understand how to use these functions together to work with structures, let’s create a simple template to display the contents of a Social-Security-number template of the type discussed earlier in this chapter. The template should empty the structure as it displays its contents

This template has four necessary stages:

1. Create and populate a structure.
2. Display the number of elements in the structure.
3. Display the contents of the structure while deleting each element.
4. Display the number of elements in the structure to verify that it is empty.

You achieve this process with the template in Listing 14.3

<!— 1. Create and populate a structure —>
<CFSET SSN – StructNew(»
<CFSET Result – StructInsert(SSN,’123-4S-6789′,’)oe Smith’»
<CFSET Result – StructInsert(SSN,’987-6S-4321′,’)ane Doe’»
<CFSET Result – StructInsert(SSN,’84S-87-234S’,’Oaryl Someone’»
<!— 2. Di5play the number of elements in the structure —>
<CFOUTPUT>
<H3>There are ‘StructCount(SSN)’ Social Secutiry
Number entries.,/H3.
</CFOUTPUT>
<HR>
<!— 3. Display the contents of the structure while deleting each element —>
<Ul>
<CFlOOP COllECTION-‘SSN’ ITEM-‘socsec’>
<CFOUTPUT><lI>’socsec’: ‘StructFind(SSN,socsec)'</CFOUTPUT>
<CFSET Result – StructOelete(SSN,socsec»
</CFlOOP>
</Ul>
<!— 4, ~isplay the number of elements in the structure :–> ~D .
<CFOUTPUT>
<H3>There are #StructCount(SSN)# Social Security
Number entries.</H3>
</CFOUTPUT>

This produces results like those in Figure 14.5

FIGURE 14,5

FIGURE 14,5

In this template, you have done the following:

• Used StructNew to create a structure called SSN.
• Used Structlnsert to add three elements to the structure with Social Security  numbers as keys and names as values,
• Used StructCount to display the number of elements in SSN,
• Used CFLOOP to loop through each entry in the structure. The COLLECTION attribute provides the contents of a structure (hence the ‘use of #SSN# as opposed to simple SSN). The ITEM attribute specifies the variable name to which the key from the structure should be assigned on each iteration through the loop
• Used Struct Find to output the value for each key in the loop.
• Used Struct Delete to delete key-value pairs from the structure ..

 

Posted on November 16, 2015 in Working with cold Fusion Data Structure

Share the Story

Back to Top
Share This