1 / 40

Array

Array. Seree Chinodom Department of Computer Science. Introduction. Student name 1 Student name 2 Student name 3 Student name 4 Student name 5. Student[1] Student[2] Student[3] Student[4] Student[5]. <CFSET CurrentStudent= student[3]> <CFOUTPUT> Student name :#student[4]#

Download Presentation

Array

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Array Seree Chinodom Department of Computer Science

  2. Introduction • Student name 1 • Student name 2 • Student name 3 • Student name 4 • Student name 5 Student[1] Student[2] Student[3] Student[4] Student[5]

  3. <CFSET CurrentStudent= student[3]> <CFOUTPUT> Student name :#student[4]# </CFOUTPUT>

  4. <CFSET student[2]= “New name”> • <CFSET index =4> • <CFSET student[index]= “New name”> • <CFSET student[index+1]= “New name2”>

  5. Multiple Dimensions • One - Dimensional Array • Two - Dimensional Array

  6. Createing Arrays • Arraynew function • one dimension <CFSET student= Arraynew(1) • Two dimesion <CFSET student= Arraynew(2)

  7. Populateing Array • One Dimension <CFSET student[1] =“ student name 1”> <CFSET student[2] =“ student name 2”> <CFSET student[3] =“ student name 3”>

  8. Populateing Array • Two Dimension <CFSET array2d[1][1] =“ value1/1”> <CFSET array2d[1][2] =“ value1/2”> <CFSET array2d[1][3] =“ value1/3”> <CFSET array2d[2][1] =“ value2/1”> <CFSET array2d[2][2] =“ value2/2”> <CFSET array2d[2][3] =“ value2/3”>

  9. Populateing Array • Three Dimension <CFSET array3d[1][1][1] =“ value1/1/1”> <CFSET array3d[1][2][1] =“ value1/2/1”> <CFSET array3d[2][1][1] =“ value2/1/1”> <CFSET array3d[2][1][2] =“ value2/1/2”> <CFSET array3d[2][2][1] =“ value2/2/1”> etc

  10. Dynamic array NameofArray[1] NameofArray[2] NameofArray[3] NameofArray[1] NameofArray[2] NameofArray[4]

  11. Dynamic array NameofArray[1] = “one” NameofArray[2] =“two” NameofArray[3] =“three” NameofArray[4] =“four” • delete NameofArray[3] =“three” NameofArray[1] = “one” NameofArray[2] =“two” NameofArray[3] =“four”

  12. Array function • ArrayAppend • ArrayPrepend • ArrayDeleteAt • ArrayInsertAt • ArrayMax • ArrayMin • ArraySum • ArrayAvg • ArraySwap • ArraySort • ArrayClear • ArrayResize • ArrayIsEmpty • IsArray • ArrayNew • ArrayToList • ListToArray • ArraySet • ArrayLen

  13. Common Array function • ArrayAppend Appends an array index to the end of the specified array. Returns a Boolean TRUE on successful completion. • Syntax ArrayAppend(array, value)

  14. ArrayPrepend • Adds an array element to the beginning of the specified array. Returns a Boolean TRUE on successful completion. • Syntax ArrayPrepend(array, value)

  15. ArrayDeleteAt • Deletes data from the specified array at the specified index position. Note that when an array index is deleted, index positions in the array are recalculated. For example, in an array containing the months of the year, deleting index position [5] removes the entry for May. If you then want to delete the entry for November, you delete index position [10], not [11], since the index positions were recalculated after index position [5] was removed.

  16. ArrayDeleteAt • Syntax ArrayDeleteAt(array, position)

  17. ArrayInsertAt • Inserts data in the specified array at the specified index position. All array elements with indexes greater than the new position are shifted right by one. The length of the array increases by one index. • Returns a Boolean TRUE on successful completion. • Syntax ArrayInsertAt(array, position, value)

  18. Function for Processing Values Stored in an Array • ArrayMax: Returns the largest numeric value in the specified array • ArrayMin: Returns the smallest numeric value in the specified array • ArraySum : Returns the sum of values in the specified array. • ArrayAvg : Returns the average of the values in the specified array.

  19. Example • ArrayName[1] = 1 • ArrayName[2] = 3 • ArrayName[3] = 2 • ArrayMax(ArrayName) -> 3 • ArrayMin(ArrayName) -> 1 • ArraySum(ArrayName) -> 6 • ArrayAvg(ArrayName) -> 2

  20. Functions for Adjusting the Position of Elements in an Array ArraySwap • Swaps array values for the specified array at the specified positions. ArraySwap can be used with greater efficiency than multiple CFSETs. • Returns a Boolean TRUE on successful completion. • Syntax ArraySwap(array, position1, position2)

  21. ArraySort ArraySort • Returns the specified array with elements numerically or alphanumerically sorted. • Syntax ArraySort(array, sort_type [, sort_order ]) • sort_type : The type of sort to execute. Sort type can be: • numeric -- Sorts numerically • text -- Sorts text alphabetically, uppercase before lowercase • textnocase -- Sorts text alphabetically; case is ignored

  22. Other Array functions • ArrayClear : Deletes all data in the specified array. Returns a Boolean TRUE on successful completion. • ArrayResize : Resets an array to a specified minimum number of elements. • ArrayIsEmpty : Determines whether the specified array is empty of data. • IsArray : Returns TRUE if value is an array. • ListToArray : Converts the specified list into an array. • ArrayToList : Converts the specified one dimensional array to a list, delimited with the character you specify.

  23. Structure Function StructNew • Returns a new structure. • Syntax StructNew()

  24. StructInsert • Inserts the specified key-value pair into the specified structure. Returns Yes if the insert was successful and • No if an error occurs. • Syntax StructInsert(structure, key, value [, allowoverwrite ])

  25. StructUpdate • Updates the specified key with the specified value. Returns Yes if the function is successful and throws an exception if an error occurs. • Syntax StructUpdate(structure, key, value)

  26. StructDelete • Removes the specified item from the specified structure. • Syntax StructDelete(structure, key [, indicatenotexisting ])

  27. StructClear • Removes all data from the specified structure. Always returns Yes. • Syntax StructClear(structure)

  28. StructCopy • Returns a new structure with all the keys and values of the specified structure. • Syntax StructCopy(structure)

  29. StructCount • Returns the number of keys in the specified structure. • Syntax StructCount(structure)

  30. StructFind • Returns the value associated with the specified key in the specified structure. • Syntax StructFind(structure, key)

  31. StructKeyExists • Returns TRUE if the specified key is in the specified structure and FALSE if it is not. • Syntax StructKeyExists(structure, key)

  32. StructIsEmpty • indicates whether the specified structure contains data. Returns TRUE if structure is empty and FALSE if it contains data. • Syntax StructIsEmpty(structure)

  33. StructKeyArray • Returns an array of the keys in the specified ColdFusion structure. • Syntax StructKeyArray(structure)

  34. StructKeyList • Returns the list of keys that are in the specified ColdFusion structure. • Syntax StructKeyList(structure, [delimiter])

  35. IsStruct • Returns TRUE if variable is a structure. • Syntax IsStruct(variable )

  36. Putting it all together • Create and populate a structure • Display the number of element in the structure • display the contents of the structure while deleting each element • display the number of element in the structure to verify that it is empty

  37. Ssn.cfm <!--- 1. Create and populate a structure ---> <CFSET SSN = StructNew()> <CFSET Result = StructInsert(SSN,"123-45-6789","Joe Smith")> <CFSET Result = StructInsert(SSN,"987-65-4321","Jane Doe")> <CFSET Result = StructInsert(SSN,"845-87-2345","Daryl Someone")> <!--- 2. Display the number of elements in the structure ---> <CFOUTPUT> <H3>There are #StructCount(SSN)# Social Security Number entries.</H3> </CFOUTPUT> <HR>

  38. <!--- 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 = StructDelete(SSN,socsec)> </CFLOOP> </UL> <!--- 4. Display the number of elements in the structure ---> <HR> <CFOUTPUT> <H3>There are #StructCount(SSN)# Social Security Number entries.</H3> </CFOUTPUT>

  39. Ssn2.cfm <!--- 1. Create and populate a structure ---> <CFSET SSN = StructNew()> <CFSET SSN["123-45-6789"]="Joe Smith"> <CFSET SSN["987-65-4321"]="Jane Doe"> <CFSET SSN["845-87-2345"]="Daryl Someone"> <!--- 2. Display the number of elements in the structure ---> <CFOUTPUT> <H3>There are #StructCount(SSN)# Social Security Number entries.</H3> </CFOUTPUT> <HR>

  40. <!--- 3. Display the contents of the structure while deleting each element ---> <UL> <CFLOOP COLLECTION=#SSN# ITEM="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 #StructCount(SSN)# Social Security Number entries</H3>. </CFOUTPUT>

More Related