Multi-dimensional Arrays

Think of multi-dimensional arrays in terms of a table holding columns and rows. To create a multi-dimensional array simply specify the number of columns and rows you want the array to have.

For example myArray(2,3) would have 3 columns and 4 rows.

The table below could represent our multidemsional array 'myArray(2,3)'.
Each cell holds a value. Below cell (0,3) holds a "bmw".

  0 1 2
0 toyota white 22.000
1 ford blue 12.500
2 porshe red 50.000
3 bmw yellow 26.000

Below is what a multidimensional array would look like that held the values in the table above;

<%
Dim myArray(2,3)
'myArray(col,row)
myArray(0,0) = "toyota"
myArray(1,0) = "white"
myArray(2,0) = "22.000"
myArray(0,1) = "ford"
myArray(1,1) = "blue"
myArray(2,1) = "12.500"
myArray(0,2) = "porshe"
myArray(1,2) = "red"
myArray(2,2) = "50.000"
myArray(0,3) = "bmw"
myArray(1,3) = "yellow"
myArray(2,3) = "26.000"
%>

Using our array 'myArray(2,3)' that we created above we could create a HTML table and display the array values with the code snippet below;

<%
Response.Write "<table border='0'>"
Response.Write "<tr><td>Row</td><td>Car</td>"
Response.Write "<td>Color</td><td>Cost</td></tr>"
'In our code below you'll notice UBound(myArray,2).
'The 2 is an optional parameter, it identifies which
'dimension of the array you want to find the highest element of.
'In this case 2 signifies the rows dimension
'1 is the default and would apply to the columns
'the highest element in the row column in this case is 3
'so we could look on our loopcounter going from 0 to 3 i.e. For i=0 to 3
'ArrayDimension defaults to 1, so if you are checking a 1 dimensional array or
'you want to check the first dimension you can leave this out.
For i = 0 to UBound(myArray,2)
Response.Write "<tr><td>" & i & "</td>"
Response.Write "<td>" & myArray(0,i) & "</td>"
Response.Write "<td>" & myArray(1,i) & "</td>"
Response.Write "<td>" & myArray(2,i) & "</td></tr>"
Next
Response.Write "</table>"
%>

The above code displays the following result:

Row  Car             Color        Cost
0       toyota         white        22.000
1       ford            blue          12.500
2       porshe        red           50.000
3       bmw           yellow       26.000

In Part 1 read about 'Arrays'
In Part 2 read about 'Array Functions'
In part 4 read about 'Dynamic Arrays'

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

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

Get your best asp web hosting provider now and save 25%