home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / array.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  5.3 KB  |  153 lines

  1. <%
  2. '*******************************************************
  3. '*     ASP 101 Sample Code - http://www.asp101.com     *
  4. '*                                                     *
  5. '*   This code is made available as a service to our   *
  6. '*      visitors and is provided strictly for the      *
  7. '*               purpose of illustration.              *
  8. '*                                                     *
  9. '* Please direct all inquiries to webmaster@asp101.com *
  10. '*******************************************************
  11. %>
  12.  
  13. <%
  14. Dim arrNames(3)     ' Declare a static array with 4 elements
  15. Dim arrAges()       ' Declare a dynamic array that we can resize
  16. Dim arrColors       ' Declare a standard variable that we can
  17.                     ' later assign an array to if we need to.
  18. %>
  19. <!--
  20. This outer table is just so you don't end up having to scroll
  21. so far down to get to the view source buttons.  It really has
  22. nothing to do with the sample.
  23. -->
  24. <table border="1">
  25. <tr>
  26. <td valign="top">
  27. <h3>Static Array</h3>
  28. <%
  29. ' Let's start with our static array.  Static doesn't mean you
  30. ' can't change the values of the elements.  It simply means you
  31. ' can change the number of elements in the array.  The values of
  32. ' the elements themselves can change as often as they need to.
  33. arrNames(0) = "Susan"
  34. arrNames(1) = "Andy"
  35. arrNames(2) = "Fred"
  36. arrNames(3) = "Kelly"
  37.  
  38. ' Notice that even though I used the number 3 to dimension the
  39. ' array there are indeed four elements which I can insert values
  40. ' into.  This is because all arrays in VBScript are zero based
  41. ' meaning the first item has an index of zero.  Geeks think in
  42. ' binary and like to be different... what can I say?
  43.  
  44. ' Display what our array currently contains. I wrapped this into
  45. ' a subroutine since I'm doing it a lot.  See the sub down at
  46. ' the bottom for implementation details.
  47. ShowArrayInTable(arrNames)
  48.  
  49. ' As I mentioned earlier... even with a static array I can
  50. ' reassign values if I need to.  I'm going to set the value of
  51. ' the second element to the value of the first element
  52. ' overwriting the existing value... effectively changing "Andy"
  53. ' to "Susan."  For good measure I'm also going to change the
  54. ' first element to "John" since I'm feeling left out.
  55. arrNames(1) = arrNames(0)
  56. arrNames(0) = "John"
  57.  
  58. ' Here's what the array now contains:
  59. ShowArrayInTable(arrNames)
  60. %>
  61. </td>
  62. <td valign="top">
  63. <h3>Dynamic Array</h3>
  64. <%
  65. ' Now on to our dynamic array.  Before I can use a dynamic array
  66. ' I need to tell it how many elements I want to be able to put
  67. ' into it.  The ReDim command allows us to redimension an array
  68. ' to whatever size we need.  I'm setting it to 2 elements so I
  69. ' use an upper bound of 1.
  70. ReDim arrAges(1)
  71.  
  72. arrAges(0) = 15
  73. arrAges(1) = 20
  74.  
  75. ' Show what our array currently contains:
  76. ShowArrayInTable(arrAges)
  77.  
  78. ' Now I'm going to ReDim the array again just to illustrate. I
  79. ' want to keep the existing data so I include the Preserve
  80. ' command.  If I didn't all the data already in the array would
  81. ' be lost.  You can also ReDim to change the number of
  82. ' dimensions in a dynamic array, but that's a little beyond the
  83. ' scope of this basic intro so here goes the simple resize up
  84. ' to 4 elements...
  85. ReDim Preserve arrAges(3)
  86.  
  87. ' Add another value:
  88. arrAges(2) = 25
  89. arrAges(3) = 30
  90.  
  91. ' Once again show what our array currently contains:
  92. ShowArrayInTable(arrAges)
  93. %>
  94. </td>
  95. <td valign="top">
  96. <h3>Array Using the Array Function</h3>
  97. <%
  98. ' Now we come to an interesting point.  Since all variables
  99. ' in VBScript are really variants, you can assign an array to
  100. ' a variable that wasn't originally defined as one!
  101. arrColors = Array("red", "green", "blue")
  102.  
  103. ' Show the values in our new array:
  104. ShowArrayInTable(arrColors)
  105. %>
  106. </td>
  107. </tr>
  108. </table>
  109.  
  110. <%
  111. ' Takes a 1 dimensional array and simply spits out its values
  112. ' in a table format with a note saying how big the array is.
  113. Sub ShowArrayInTable(ArrayToShow)
  114.     Dim I           ' Simple Looping Var
  115.     Dim iArraySize  ' Var to store array size
  116.  
  117.     ' If you want to know how big an array is, you can use this
  118.     ' to find out. This even works in VB where they don't have
  119.     ' to be zero-based.  The LBound and UBound return the
  120.     ' indecies of the lowest and highest array elements so to
  121.     ' get the size we take the difference and add one since you
  122.     ' can store a value at both end points.
  123.     iArraySize = (UBound(ArrayToShow) - LBound(ArrayToShow)) + 1
  124.  
  125.     Response.Write "<p>The array has " & iArraySize _
  126.         & " elements.  They are:</p>" & vbCrLf
  127.     
  128.     Response.Write "<table border=""1"">" & vbCrLf
  129.  
  130.     Response.Write "<thead>" & vbCrLf
  131.     Response.Write "<tr>" & vbCrLf
  132.     Response.Write "<th>Index</th>" & vbCrLf
  133.     Response.Write "<th>Value</th>" & vbCrLf
  134.     Response.Write "</tr>" & vbCrLf
  135.     Response.Write "</thead>" & vbCrLf
  136.  
  137.     Response.Write "<tbody>" & vbCrLf
  138.     
  139.     ' Simple loop over a table outputting a row for each element
  140.     For I = LBound(ArrayToShow) To UBound(ArrayToShow)
  141.         Response.Write "<tr>" & vbCrLf
  142.         ' Write out the index of the element we're currently on
  143.         Response.Write "<td>" & I & "</td>" & vbCrLf
  144.         ' Write out the value of the element we're currently on
  145.         Response.Write "<td>" & ArrayToShow(I) & "</td>" & vbCrLf
  146.         Response.Write "</tr>" & vbCrLf
  147.     Next 'I
  148.     Response.Write "</tbody>" & vbCrLf
  149.  
  150.     Response.Write "</table>" & vbCrLf
  151. End Sub
  152. %>
  153.