Asp and Arrays

Who I am
Lluis Enric Mayans
@lluisenricmayans
Author and references

In ASP we can consider a array as a data format corresponding to a list of elements where each element is distinguished by a pair of key and value.

As a rule, it is customary to collect under the computer term array both the mathematical elements corresponding to vectors and matrices. Arrays naturally start from zero (0) and not from one (1), but we can also manage them starting from the value we are interested in or even manipulate them with non-consecutive values.



However, let's see a first example:

Dim myArray(1) myArray(0) = "this" myArray(1) = "that" ReDim Preserve myArray(2) myArray(2) = "theother".

Arrays are indexed to zero, this means if I write as above Dim my Array (1) it means I have reserved two lines" of our array, row 0 and row 1.

to vary the size of an array while keeping the previous content unchanged (unless you shorten the size of the array of course), I use Redim with the clause Preserve identical to how it happened in Visual Basic.

HOW TO OPERATE WITH ONE DIMENSIONAL ARRAYS
 

-Split-

Example:

Dim MyString, MyArray MyString = "Paola,Mario,Luca" MyArray = Split(MyString, ",")

-LBound/ UBound-
It returns me the lowest and highest value of the specified array respectively. The maximum number of elements available in the array, in addition to the ArrayName, you can specify the element to check as better explained below.

LBound(ArrayName).

 

MULTIDIMENSIONAL ARRAYS
Of intuitive syntax they declare themselves as follows: Dim MyArray (x, y)  where x = columns and y = rows. Here is a clear example of how you can use a multidimensional array:



Dim myArray(2,3) 'myArray(col,row) 'Array def is (dept,item,cost) myArray(0,0) = "housewares" myArray(1,0) = "sauce pan" myArray(2,0) = "22.50" myArray(0,1) = "housewares" myArray(1,1) = "toaster" myArray(2,1) = "12.50" myArray(0,2) = "housewares" myArray(1,2) = "wooden spoon" myArray(2,2) = "4.50" myArray(0,3) = "housewares" myArray(1,3) = "oven cleaner" myArray(2,3) = "2.50" Response.Write("") Response.Write("RowDepartment") Response.Write("Item NameCost") For i = 2 to UBound(myArray, 0) Response.Write("#" & i & "") Response.Write("" & myArray(2,i) & "") Response.Write("" & myArray(0,i) & "") Response.Write("" & myArray(1,i) & "") Next Response.Write("")


Here is the result:

Row Department Item Name Cost
0 housewares sauce pan 22.50
1 housewares toaster 12.50
2 housewares wooden spoon 4.50
3 housewares oven cleaner 2.50


The only tricky step in this code is the step where the cycle is taken into account The:

UBound(MyArray, 2) it means that I take the greater value of the second element of the array (ie the maximum number of rows, “2”). In other words, the “For” loop is used to fill the whole table up
to the last available line.


 

MULTIDIMENSIONAL RECORDSET AND ARRAYS

Let's see the code immediately:

sql = "select * from myTable" Set RS = Conn.Execute(sql) 'Mette il Recordset nell'array Dim myArray() numRows = 0 Do While NOT RS.EOF numRows = numRows + 1 ReDim Preserve myArray(3, numRows) myArray(0, numRows - 1) = RS(0) myArray(1, numRows - 1) = RS(1) myArray(2, numRows - 1) = RS(2) myArray(3, numRows - 1) = RS(3) RS.MoveNext Loop

Hello

add a comment of Asp and Arrays
Comment sent successfully! We will review it in the next few hours.