ASP array is a relatively easy-to-use container for loading large amounts of data.
1. Define array
There are two ways: DIM and REDIM.
DIM defines an array of fixed number and data type; REDIM is different. It can define different types of data, and it can also define data whose number is not fixed. Compare the following examples. Examples that are all legal:
program code
Dim myarray(5,2)
Redim myarray(5,2)
Examples where the former is wrong and the latter is legal:
program code
n=10
Dim myarray(n)
Redim myarray(n,2)
In addition, REDIM can also define arrays of undetermined types, such as:
program code
Redim myarray(10)
2. Number of arrays
The subscript specified when defining an array with DIM or REDIM represents the maximum subscript allowed when accessing the array, but not the number of arrays. In fact, the number of one-dimensional arrays is always equal to (maximum subscript + 1), and access is performed one by one starting from 0 through the subscript.
for example:
program code
Dim myarray(5)
There are 6 defined array elements, namely:
Quote content
myarray(0), myarray(1), myarray(2), myarray(3), myarray(4), myarray(5)
Another example:
program code
Redim thisarray(2,5)
In fact, a two-dimensional array of (2+1)*(5+1)=1 8 is defined.
In this case, can we define an array with only one element unambiguously? The answer is: no.
As mentioned before,
program code
Redim thisarray(1)
The array defined actually has (1+1) array elements, but looks like:
program code
Redim thisarray(0)
The syntax is wrong. Therefore, you cannot define an array with only one array element. In fact, what is mentioned above is just its default status. In fact, when defining an array, you can define the number of arrays and even the starting and ending numbers of the subscripts by defining the start and end of the subscripts. for example:
program code
Redim thisarray(1980 to1990)
An array containing 11 elements is defined, with subscripts from 1980 to 1990.
3. About UBOUND function
UBOUND returns the maximum subscript of a one-dimensional array, not the number of elements. for example:
program code
Dim Myarray(5)
,So
program code
UBOUND(Myarray)
The value returned is 5, not 6. UBOUND can also be applied to two-dimensional arrays. When applied to a two-dimensional array, it returns the maximum value of the first index.
for example:
program code
Dim Myarray(6,3)
, So
program code
UBOUND(Myarray)
The returned value is 6, not 7, let alone 18 (6*3=18).
To return the maximum value of the second subscript, use:
program code
UBOUND(Myarray,2)
.
Corresponding to UBOUND is another function: LBOUND, which returns the minimum subscript of the array. Similar to UBOUND, LBOUND(Myarray,2) returns the minimum value of the second subscript of the array MYARRAY. So, to be precise, the number of elements of the one-dimensional array Myarray is:
program code
UBOUND(Myarray)-LBOUND(Myarray)+1
, and the number of elements of the two-dimensional array is:
program code
(UBOUND(Myarray)-LBOUND(Myarray)+1)*(UBOUND(Myarray,2)-LBOUND(Myarray,2)+1)
http://www.knowsky.com/
Multidimensional arrays and so on.
4. Definition of array
program code
DimMyArray
MyArray = Array(1,5,123,12,98)
Expandable array
program code
DimMyArray()
for i = 0 to 10
ReDim PReserve MyArray(i)
MyArray(i)=i
next
Splits a string and returns an array of split results
program code
DimMyArray
MyArray = Split(tempcnt,chr(13)&chr(10))
For I = Lbound(MyArray) to Ubound(MyArray)
Response.Write MyArray(I) & <br>
Next
5. Array sorting function
program code
Function Sort(ary)
KeepChecking = TRUE
Do Until KeepChecking = FALSE
KeepChecking = FALSE
For I = 0 to UBound(ary)
If I = UBound(ary) Then Exit For
If ary(I) > ary(I+1) Then
FirstValue = ary(I)
SecondValue = ary(I+1)
ary(I) = SecondValue
ary(I+1) = FirstValue
KeepChecking = TRUE
End If
Next
Loop
Sort = ary
End Function
Array sorting function application example
program code
DimMyArray
MyArray = Array(1,5,123,12,98)
MyArray = Sort(MyArray)
For I = Lbound(MyArray) to Ubound(MyArray)
Response.Write MyArray(I) & <br>
Next
6. Use arrays in application and session
program code
Application.Lock
Application(StoredArray) = MyArray
Application.Unlock
LocalArray = Application(StoredArray)
Overwrite array in Application
program code
Application.Lock
Application(StoredArray) = LocalArray
Application.Unlock
Session usage is the same as Application
7. Import data from the database into the array
This method is often used by me in function integration of code.
program code
DimMyArray
Get all records
MyArray = RS.GetRows
Get the first 10 records
MyArray = RS.GetRows(10)
For row = 0 To UBound(MyArray, 2)
For col = 0 To UBound(MyArray, 1)
Response.Write (col, row) & <br>
Next
Next