ASP Arrays

In short arrays are variables that can store more than one value. They are used to store a series of related information.
Lets create an array called 'myArray' that will hold the names of 7 soccer teams.

Arrays in ASP/VBScript have a zero based index. This means that the first item in the array starts at 0.

<%
'Lets create our Array. First lets declare our Array variable
Dim myArray(6)

myArray(0)="spurs"
myArray(1)="celtic"
myArray(2)="ipswich"
myArray(3)="liverpool"
myArray(4)="man utd"
myArray(5)="ac milan"
myArray(6)="juventus"
'Now lets create a loop moving through the array and printing out the values
'In the loop below we will simply substitute the value of i in myArray(i)
'for the first loop through i will be 0, then 1 and so on until it passes through 6
For i=0 to 6
response.write myArray(i) & "<br>"
Next     'move on to the next value of i
%>

Output:

spurs
celtic
ipswich
liverpool
man utd
ac milan
juventus


In the example code above we have defined values for each individual array element on separate lines. Another way to create an array especially for smaller arrays is to define the whole array in one line with each element separated by a comma.

<%
Dim myArray
myArray = Array("spurs","celtic","ipswich","brazil")
%>

Again we could loop through the array and print out the values in each array element.

<%
Dim myArray
myArray = Array("spurs","celtic","ipswich","brazil")
For i=0 to 3
response.write myArray(i) & "<br>"
Next     'move on to the next value of i
%>

Assigning an array size as in the example code below with a variable will result in an error. You have to use an integer unless it's a dynamic array.

<%
myArraySize=10
Dim myArray(myArraySize)
%>

In part 2 read about 'Array Functions'
In part 3 read about 'Multidimensional Arrays'
In part 4 read about 'Dynamic Arrays'



Get the best ASP hosting with DiscountASP.NET - great value, money back guarantee.

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.

Global ASP.NET Hosting Leader - Click Here