Accessing Structures Using Associatlve Arr Syntax ColdFusion Help

The associative-array syntax for accessing elements in a structure is designed. to r the syntax used to access elements of standard arrays. This syntax uses square brae around the index or key. Therefore in our social security number structure you c assign a key-value pair to the structure with the following

<CFSET SSN[‘123-45-6789’] – ‘Joe Smith’>

In fact, several parts of the template you just used to display and delete entries f the social security number structure could be written using associative-array syntrather than using the functions, as shown in listing 14.4.

<!— 1. Create and populate a structure —>
<CFSET SSN – StructNew(» ‘
<CFSET SSN[‘123-4S-6789’]-‘Joe Smith’>
<CFSET SSN[‘987-6S-4321’]-‘Jane Doe’>
” <CFSET SSN[’84S-87-234S’]c’Daryl Someone’>
<!— 2. Display the number of elements in the structure —>
<CFOUTPUT>
<H3>There are #StructCount(SSN)# Social Security
Number entries.</H3>
</CFOUTPUT>
<HR>
<!— 3. Display the contents of the structure while de~eting each element —
<Ul>
<CFlOOP COllECTIONc#SSN# ITEMs’socsec’>
<CFOUTPUT><lI>#socsec#: #SSN[socsec)#</CFOUTPUT>
<CFSET Result – StructDelete(SSN,socsec»
</CFlOOP>
</UL>
<!— 4. Display the number of elements in the structure —>
<HR>
<CFOUTPUT>
<H3>There are lStructCount(SSN)I Social Secutiry
Number entries</H3>.
</CFOUTPUT>

You keep StruetNew StruetCount. and StruetDe 1ete because these functions perform tasks that require the use of functions However your eplace Stuct Inser-t with the use of anassignmentsuch as SS~(‘987-65-4321′]’ – •Jane Doe’. Similarly StruetFind is ~l~ced with a simple reference to an element in the structure: SSN[soesee].

Using Object Property Notation

The third alternative for accessing elements in a structure is object-property notation which is borrowed from object-oriented programming languages such as Java and c++. In these languages objects can have properties that can be referred to with:

Objectname.propertyname

In the context of Coldfusion structures the object corresponds to the structure and the property name corresponds to the key of a key-value pair. Therefore where you used the following syntax

Structure(‘Key’] – Value

you can also use:

Structure.key – Value

This syntax has a limitation: The key cannot contain special characters such as numbers spaces, or punctuation. The keys should be plain text strings

Therefore, our social security number example as written cannot be altered to use object property notation. This is because the keys contain hyphens. If you try to use object-property notation-for instance, by using the following assignment:

SSN.IZ3-4S-6789 – ‘Joe Smith’

you will get an error.

Where Do We Go from Here?

In this chapter you have covered the last major types of data structures available in ColdFusion. These included the types first discussed in Chapter 4 “Creating and Manipulating Variables” as well as the lists arrays and structures introduced in this chapter.

In the next chapter you ‘will move into a major area of interactivity: forms in Cold-Fusion. You will learn how to build forms using special ColdFusion tags as well as hov to process the data from form submissions

It is this capability to easily create, populate, and process forms that makes Cold-Fusion a powerful tool for developing interactive applications. It enables you to obtain information from the user for the purpose of returning dynamic content and it enables users to interact with and even alter data

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

Share the Story

Back to Top
Share This