• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Step 10. Using Lists, Arrays, and Struct... > Example 10.3: Working with Array Ele...

Example 10.3: Working with Array Elements

In this example, we will look at various techniques for inserting and deleting elements within an array.

1.
Open your text editor and type the code in Listing 10.4 or open the Array2.cfm file from the CompletedFiles\Examples\Step10 folder.

Listing 10.4. Array2.cfm

<!---
File:             Array2.cfm 
Description:      Demo the working with array elements 
Author: 
Created: 
---> 
<HTML> 
  <HEAD> 
      <TITLE>Array 2</TITLE> 
  </HEAD> 
  <BODY> 
    <!--- create the array ---> 
    <CFSET aDepts=ArrayNew(1)> 
      <TABLE BORDER="1" CELLSPACING="4"> 
            <TR VALIGN="top"> 
                  <TD> 
                       <!--- populate the array with data ---> 
                       <CFSET aDepts[1]="Marketing"> 
                       <CFSET aDepts[2]="Operations"> 
                       <CFSET aDepts[3]="Sales"> 
                       <CFSET aDepts[4]="Technology"> 
                       <P>Array Has Been Populated</P> 
                       <!--- dump the array ---> 
                       <CFDUMP VAR="#aDepts#"> 
                  </TD> 
                  <TD> 
                       <CFSET temp=ArrayInsertAt(aDepts,3,"Legal")> 
                       <P>'Legal' Has Been Inserted</P> 
                       <!--- dump the array ---> 
                       <CFDUMP VAR="#aDepts#"> 

                  </TD> 
                  <TD> 
                       <CFSET temp=ArrayDeleteAt(aDepts,1)> 
                       <P>'Marketing' Has Been Deleted</P> 
                       <!--- dump the array ---> 
                       <CFDUMP VAR="#aDepts#"> 
                  </TD> 
                  <TD> 
                       <CFSET temp=ArrayClear(aDepts)> 
                       <P>The Array Has Been Cleared</P> 
                       <!--- dump the array ---> 
                       <CFDUMP VAR="#aDepts#"> 
                  </TD> 
                  <TD> 
                       <CFSET temp=ArraySet(aDepts,1,5,"full")> 
                       <P>Use ArraySet to prefill the array</P> 
                       <!--- dump the array ---> 
                       <CFDUMP VAR="#aDepts#"> 
                  </TD> 
            </TR> 
      </TABLE> 
   </BODY> 
</HTML>

					  

2.
Save the file as Array2.cfm in your Example\Step10 folder.

3.
Browse to the Array2.cfm file. You should see something similar to Figure 10.6.

Figure 10.6. The Array2.cfm browser display.



PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint