<% '******************************************************* '* ASP 101 Sample Code - http://www.asp101.com/ * '* * '* This code is made available as a service to our * '* visitors and is provided strictly for the * '* purpose of illustration. * '* * '* http://www.asp101.com/samples/license.asp * '* * '* Please direct all inquiries to webmaster@asp101.com * '******************************************************* %> <% Dim arrNames(3) ' Declare a static array with 4 elements Dim arrAges() ' Declare a dynamic array that we can resize Dim arrColors ' Declare a standard variable that we can ' later assign an array to if we need to. %>

Static Array

<% ' Let's start with our static array. Static doesn't mean you ' can't change the values of the elements. It simply means you ' can't change the number of elements in the array. The values ' of the elements themselves can change as often as they need to. arrNames(0) = "Susan" arrNames(1) = "Andy" arrNames(2) = "Fred" arrNames(3) = "Kelly" ' Notice that even though I used the number 3 to dimension the ' array there are indeed four elements which I can insert values ' into. This is because all arrays in VBScript are zero based ' meaning the first item has an index of zero. Geeks think in ' binary and like to be different... what can I say? ' Display what our array currently contains. I wrapped this into ' a subroutine since I'm doing it a lot. See the sub down at ' the bottom for implementation details. ShowArrayInTable(arrNames) ' As I mentioned earlier... even with a static array I can ' reassign values if I need to. I'm going to set the value of ' the second element to the value of the first element ' overwriting the existing value... effectively changing "Andy" ' to "Susan." For good measure I'm also going to change the ' first element to "John" since I'm feeling left out. arrNames(1) = arrNames(0) arrNames(0) = "John" ' Here's what the array now contains: ShowArrayInTable(arrNames) %>

Dynamic Array

<% ' Now on to our dynamic array. Before I can use a dynamic array ' I need to tell it how many elements I want to be able to put ' into it. The ReDim command allows us to redimension an array ' to whatever size we need. I'm setting it to 2 elements so I ' use an upper bound of 1. ReDim arrAges(1) arrAges(0) = 15 arrAges(1) = 20 ' Show what our array currently contains: ShowArrayInTable(arrAges) ' Now I'm going to ReDim the array again just to illustrate. I ' want to keep the existing data so I include the Preserve ' command. If I didn't all the data already in the array would ' be lost. You can also ReDim to change the number of ' dimensions in a dynamic array, but that's a little beyond the ' scope of this basic intro so here goes the simple resize up ' to 4 elements... ReDim Preserve arrAges(3) ' Add another value: arrAges(2) = 25 arrAges(3) = 30 ' Once again show what our array currently contains: ShowArrayInTable(arrAges) %>

Array Using the Array Function

<% ' Now we come to an interesting point. Since all variables ' in VBScript are really variants, you can assign an array to ' a variable that wasn't originally defined as one! arrColors = Array("red", "green", "blue") ' Show the values in our new array: ShowArrayInTable(arrColors) %>
<% ' Takes a 1 dimensional array and simply spits out its values ' in a table format with a note saying how big the array is. Sub ShowArrayInTable(ArrayToShow) Dim I ' Simple Looping Var Dim iArraySize ' Var to store array size ' If you want to know how big an array is, you can use this ' to find out. This even works in VB where they don't have ' to be zero-based. The LBound and UBound return the ' indecies of the lowest and highest array elements so to ' get the size we take the difference and add one since you ' can store a value at both end points. iArraySize = (UBound(ArrayToShow) - LBound(ArrayToShow)) + 1 Response.Write "

The array has " & iArraySize _ & " elements. They are:

" & vbCrLf Response.Write "" & vbCrLf Response.Write "" & vbCrLf Response.Write "" & vbCrLf Response.Write "" & vbCrLf Response.Write "" & vbCrLf Response.Write "" & vbCrLf Response.Write "" & vbCrLf Response.Write "" & vbCrLf ' Simple loop over a table outputting a row for each element For I = LBound(ArrayToShow) To UBound(ArrayToShow) Response.Write "" & vbCrLf ' Write out the index of the element we're currently on Response.Write "" & vbCrLf ' Write out the value of the element we're currently on Response.Write "" & vbCrLf Response.Write "" & vbCrLf Next 'I Response.Write "" & vbCrLf Response.Write "
" & I & "" & ArrayToShow(I) & "
" & vbCrLf End Sub %>