Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Chapter 8 Complex Data Types Overview In previous chapters, only simple data types such as text or numeric were used in the creation of web applications. Chapter 8 delves into the world of complex data types such as lists, arrays, and structures. Throughout the chapter, each data type is discussed and scenarios of using the data type are explored. In addition to providing an understanding of each data type, complex data type manipulation functions are outlined in detail as well. Multiple ColdFusion instructions may be grouped together using server-side scripting which eliminates the need for typing repetitive tag style lines of code. Using this technique reduces coding time and reduces the learning curve for programmers who are familiar with the C++ style of coding. Teaching Tips and Strategies To practice syntax of lists and arrays, have the students rewrite Example 8.1 in the chapter using an array instead of a list. As a group you may discuss the differences in syntax between arrays and lists as well as reasons why you would choose to use a list versus an array. When having the students type in and run the exercises presented throughout the chapter (which is a good idea), it is important that they discover what is actually taking place. It may be a good idea to have a group discussion on each of the templates shown in the chapter. Be sure to dissect the lines of code – especially discussing the lines of code that are highlighted as they are new topics for this chapter. Lecture Notes I. Lists – created by assigning the list values to a variable A. ListFirst – returns the first element in a list B. ListLast – returns the last element in a list C. ListLen – returns the number of elements in a list D. ListAppend – appends a value to a list E. ListInsertAt – Inserts a value in the position specified F. ListPrepend – inserts a value at the beginning of the list G. ListGetAt – returns the element in the position specified H. ListDeleteAt – deletes the element in the position specified II. Arrays – differs from traditional arrays where the array size is fixed A. Create a new array using <cfset>, for example <cfset nameArray = ArrayNew(n)> where n specifies the dimension of the array B. Refer to an array element using nameArray[n] where n is the desired element of the array C. Set the elements of the array using <cfset>, for example <cfset nameArray[n] = “value” where n is the desired element in the array ColdFusion Chapter 8: Complex Data Types D. Refer to a two-dimensional array using nameArray[n1][n2] where n1 is the element in the first dimension and n2 is the element in the second dimension E. Array Manipulation functions: 1. ArrayAppend – creates a new array element at the end of an array 2. ArrayPrepend – creates a new array element at the beginning of an array 3. ArrayInsertAt – inserts an array element at the index position specified 4. ArrayDeleteAt – deletes an array element at the index position specified 5. ArrayIsEmpty – returns True if the array has no data 6. ArrayLen – returns the number of elements in an array 7. IsArray – returns True if the parameter is an array 8. ArrayToList – converts an array to a list delimited with the character specified 9. ListToArray – converts a list delimited with the character specified to an array 10. ArrayClear – clears all data in the array specified F. Looping through an array can be achieved using <cfloop> or nested <cfloop> statements if multi-dimensional III. Structures – provide mechanism for representing complex data by grouping under a single entity A. Create a structure using <cfset structName=structNew()> B. Property notation – using the object.property syntax; for example, structName.keyname C. Assigning a value to a structure property can be achieved using <cfset>; for example, <cfset structName.keyname=”value”> D. Associative Array notation – allows the use string indices with an array rather than using numbers; for example, <cfset structName[“Name”]=”value”> E. Structure Functions: 1. StructClear – deletes all data from the structure 2. StructInsert – inserts a key-value pair into a structure 3. StructUpdate – modifies the value associated with the key 4. StructFind – returns the value associated with a key 5. StructCount – returns the number of keys in a structure 6. IsStruct – returns True if the variable is a structure F. To loop through a structure’s elements, use the <cfloop> tag using the collection object IV. Server-side scripting using <cfscript> tag – allows a block of ColdFusion code to be written and processed without using the tag style of programming. A. Comments are written with the // symbol or between /* */ CF_IM08-2 ColdFusion Chapter 8: Complex Data Types Review Questions Answers: 1. Three complex data types in ColdFusion are: Lists, Arrays, and Structures. 2. The advantages of using complex data types: 1. Complex data types help you manage complex data within your application. 2. You can make changes to your data in memory without repeated database transactions. 3. Transactions may be committed to the database only after the data in memory is complete. 3. The default delimiter for list elements is the comma ‘,’. 4. You can create a list by assigning a value (string) to a variable. Lists are basically strings that have delimiters. Examples: Delimiter is a space: <cfset myList = “This is my book”> Delimiter is a comma: <cfset myList = “Jane, Mary, Paul, Peter”> 5. A dynamic array is an array that expands as you add data to it and contracts as you remove data from it. 6. To create a new array in ColdFusion we use the ArrayNew() function. Examples: One-dimensional array: <cfset myArray = ArrayNew(1)> Two-dimensional array: <cfset myArray = ArrayNew(2)> 7. ColdFusion structures are complex data types that provide a powerful and flexible mechanism for representing complex data by allowing you to group related data under a single entity. Structures consist of key-value pairs. 8. The StructNew() function is used to create a new array. Example: <cfset student=structnew()> 9. Two notations that can be used to reference structures in ColdFusion are: Property Notation: <cfset student.firstname=”Mary”> <cfoutput>#student.firstname#</cfoutput> Associative Array Notation: <cfset student[“firstname”]=”Mary”> CF_IM08-3 ColdFusion Chapter 8: Complex Data Types <cfoutput>#student[“firstName”]#</cfoutput> 10. <cfscript> is the server-side scripting language in ColdFusion. 11. Advantage of using this language: fewer lines of code and easier coding for some (JavaScript like syntax). 12. To output values using <cfscript> we use the writeOutput() function. Example: <cfscript> WriteOutput(“Hello World”); </cfscript> 13. The main difference between client-side and server-side scripting is that client-side scripts run on the client’s browser and server-side scripts interact with the server and output web pages. Client-side scripts run faster as they do not interact with the server. Server-side scripts are not browser dependent and are more flexible than client-side scripting languages. 14. I would use a structure with the element’s product name, price, stock on hand, and category. 15. Example 1: Student’s courses list – structure would contain course name and other course details. The array of structures would hold the list of all courses the student takes. Example 2: Multiple shipping addresses of a customer. Structure would contain the address fields and the array would hold the list of the shipping addresses stored by the customer. 16. StructUpdate() is used to update values in a structure. True/False Answers: 1. False: The default delimiter for list elements is a comma. 2. True 3. False – CFScript is a server side scripting language. 4. True 5. False – ColdFusion does support up to two dimensional dynamic arrays. 6. True 7. False – it is case insensitive. CF_IM08-4 ColdFusion Chapter 8: Complex Data Types Programming Exercises Answers: 1. See ch08ex01sol.cfm 2. See ch08ex02sol.cfm 3. See ch08ex03sol.cfm and Application.cfm 4. See ch08ex04sol.cfm 5. Line 1, the dimension is not mentioned. <cfset session.courseList = arrayNew(1)> Project Building Exercises Answers: 1. Done as part of final Shopping Cart project (Phase 2). See shoppingCart.cfm. 2. Done as part of final Shopping Cart project (Phase 2). See review.cfm 3. Done as part of final Shopping Cart project (Phase 2). See saveorder.cfm CF_IM08-5