home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 February / CHIPCD_02_2002.iso / Internet / Macromedia ColdFusion Server 5 / coldfusion-50-win-us.exe / data1.cab / Examples / CFDOCS / snippets / structcopy.cfm < prev    next >
Encoding:
Text File  |  2001-06-13  |  4.4 KB  |  161 lines

  1.  <!------------------------------------------------------------------ 
  2.  This example shows the differences between the StructCopy and the 
  3.  Duplicate functions. 
  4.  ------------------------------------------------------------------->
  5. <HTML>
  6. <HEAD>
  7. <TITLE>
  8. StructCopy Example
  9. </TITLE>
  10. </HEAD>
  11.  
  12. <BASEFONT FACE="Arial, Helvetica" SIZE=2>
  13. <BODY  bgcolor="#FFFFD5">
  14.  
  15. <H3>StructCopy Example</H3>
  16.  
  17. <!----------------------------------------------------------------------
  18. The following code shows examples of assignment by-value and assignment 
  19. by-reference.
  20. ----------------------------------------------------------------------->
  21.  
  22. // This script creates a structure that StructCopy copies by value. <br> 
  23.  
  24. <CFSCRIPT>
  25.     // Create elements.
  26.     s = StructNew();
  27.     s.array = ArrayNew(2);
  28.  
  29.     // Assign simple values to original top-level structure fields.
  30.     s.number = 99;
  31.     s.string = "hello tommy";
  32.  
  33.     // Assign values to original top-level array.
  34.     s.array[1][1] = "one one";
  35.     s.array[1][2] = "one two";
  36. </CFSCRIPT>
  37.  
  38. <!--- Output original structure --->
  39. <hr>
  40. <b>Original Values</b><br>
  41. <CFOUTPUT>
  42.     // Simple values <br>
  43.     s.number = #s.number#<br>
  44.     s.string = #s.string#<br>
  45.     // Array value <br>
  46.     s.array[1][1] = #s.array[1][1]#<br>
  47.     s.array[1][2] = #s.array[1][2]#<br>
  48. </CFOUTPUT>
  49.  
  50. // Copy this structure to a new structure. <br>
  51.  
  52. <CFSET copied = StructCopy(s)>
  53.  
  54. <CFSCRIPT>
  55. // Change the values of the original structure. <br>
  56.     s.number = 100;
  57.     s.string = "hello tommy (modified)";
  58.     s.array[1][1] = "one one (modified)";
  59.     s.array[1][2] = "one two (modified)";
  60. </CFSCRIPT>
  61.  
  62. <hr>
  63. <b>Modified Original Values</b><br>
  64. <CFOUTPUT>
  65.     // Simple values <br>
  66.     s.number = #s.number#<br>
  67.     s.string = #s.string#<br>
  68.     // Array value <br>
  69.     s.array[1][1] = #s.array[1][1]#<br>
  70.     s.array[1][2] = #s.array[1][2]#<br>
  71. </CFOUTPUT>
  72.  
  73. <hr>
  74. <b>Copied structure values should be the same as the original.</b><br>
  75. <CFOUTPUT>
  76.     // Simple values <br>
  77.     copied.number = #copied.number#<br>
  78.     copied.string = #copied.string#<br>
  79.     // Array value <br>
  80.     copied.array[1][1] = #copied.array[1][1]#<br>
  81.     copied.array[1][2] = #copied.array[1][2]#<br>
  82. </CFOUTPUT>
  83.  
  84. // This script creates a structure that StructCopy copies by reference.  
  85.  
  86. <CFSCRIPT>
  87.     // Create elements.
  88.     s = StructNew();
  89.     s.nested =StructNew();
  90.     s.nested.array = ArrayNew(2);
  91.  
  92.     // Assign simple values to nested structure fields.
  93.     s.nested.number = 99;
  94.     s.nested.string = "hello tommy";
  95.  
  96.     // Assign values to nested array.
  97.     s.nested.array[1][1] = "one one";
  98.     s.nested.array[1][2] = "one two";
  99. </CFSCRIPT>
  100.  
  101. <!--- Output original structure --->
  102. <hr>
  103. <b>Original Values</b><br>
  104. <CFOUTPUT>
  105.     // Simple values <br>
  106.     s.nested.number = #s.nested.number#<br>
  107.     s.nested.string = #s.nested.string#<br>
  108.  
  109.     // Array values <br>
  110.     s.nested.array[1][1] = #s.nested.array[1][1]#<br>
  111.     s.nested.array[1][2] = #s.nested.array[1][2]#<br>
  112. </CFOUTPUT>
  113.  
  114. // Use StructCopy to copy this structure to a new structure. <br>
  115. <CFSET copied = StructCopy(s)>
  116. // Use Duplicate to clone this structure to a new structure. <br>
  117. <CFSET duplicated = Duplicate(s)>
  118.  
  119. <CFSCRIPT>
  120. // Change the values of the original structure.
  121.     s.nested.number = 100;
  122.     s.nested.string = "hello tommy (modified)";
  123.     s.nested.array[1][1] = "one one (modified)";
  124.     s.nested.array[1][2] = "one two (modified)";
  125. </CFSCRIPT>
  126. <hr>
  127. <b>Modified Original Values</b><br>
  128. <CFOUTPUT>
  129.     // Simple values <br>
  130.     s.nested.number = #s.nested.number#<br>
  131.     s.nested.string = #s.nested.string#<br>
  132.  
  133.     // Array value <br>
  134.     s.nested.array[1][1] = #s.nested.array[1][1]#<br>
  135.     s.nested.array[1][2] = #s.nested.array[1][2]#<br>
  136. </CFOUTPUT>
  137.  
  138. <hr>
  139. <b>Copied structure values should reflect changes to original.</b><br>
  140. <CFOUTPUT>
  141.     // Simple values <br>
  142.     copied.nested.number = #copied.nested.number#<br>
  143.     copied.nested.string = #copied.nested.string#<br>
  144.     // Array values <br>
  145.     copied.nested.array[1][1] = #copied.nested.array[1][1]#<br>
  146.     copied.nested.array[1][2] = #copied.nested.array[1][2]#<br>
  147. </CFOUTPUT>
  148.  
  149. <hr>
  150. <b>Duplicated structure values should remain unchanged.</b><br>
  151. <CFOUTPUT>
  152.     // Simple values <br>
  153.     duplicated.nested.number = #duplicated.nested.number#<br>
  154.     duplicated.nested.string = #duplicated.nested.string#<br>
  155.     // Array value <br>
  156.     duplicated.nested.array[1][1] = #duplicated.nested.array[1][1]#<br>
  157.     duplicated.nested.array[1][2] = #duplicated.nested.array[1][2]#<br>
  158. </CFOUTPUT>
  159.  
  160. </BODY>
  161. </HTML>