ASP array is a good container for loading large amounts of data.
1 Define an array
There are two methods: DIM and REDIM.
DIM defines arrays of fixed numbers and data types, while REDIM defines different types of data, or the number is not fixed. Compare the following examples. Examples:
Dim myarray (5, 2)
Redim myarray (5, 2)
The former is incorrect while the latter is valid. Example:
N = 10 n = 10
Dim myarray (n)
Redim myarray (n, 2)
In addition, REDIM can also define undefined arrays, such as Redim myarray (10)
2. Number of Arrays
The subscript specified when an array is defined by DIM or REDIM indicates the maximum subscript allowed to access the array, but not the number of the array. In fact, the number of one-dimensional arrays is always equal to (the maximum subscript + 1). During access, the numbers are accessed one by one from 0 through the subscript.
For example, there are 6 array elements defined by Dim myarray (5): myarray (0), myarray (1), myarray (2), and myarray (3), myarray (4), and myarray (5 ).
Another example: Redim thisarray () actually defines a two-dimensional array (2 + 1) * (5 + 1) = 1 8.
In this case, it is not necessary to define an array with only one element? The answer is: no.
As mentioned above, the array defined by Redim thisarray (1) actually has (1 + 1) array elements, but is similar to the syntax of Redim thisarray (0), which is incorrect. Therefore, an array with only one array element cannot be defined. In fact, the above is only about its default situation. In fact, to define an array, you can define the starting and ending numbers of the lower mark so as to define the number of arrays or even the starting and ending numbers of the lower mark. For example, Redim thisarray (1980 to1990) defines an array containing 11 elements with subscripts ranging from 1980 to 1990.
3 about UBOUND Functions
UBOUND returns the maximum subscript of a one-dimensional array, rather than the number of elements. For example: Dim Myarray (5), then the value returned by UBOUND (Myarray) 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 submark.
For example, Dim Myarray (6, 3 ),
Then the value returned by UBOUND (Myarray) is 6, not 7, not 18 (6*3 = 18 ).
To return the maximum value of the second subobject, use UBOUND (Myarray, 2 ).
Opposite 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 lower mark of the array MYARRAY. Therefore, the number of elements in the one-dimensional array Myarray is UBOUND (Myarray)-LBOUND (Myarray) + 1, while the number of elements in the two-dimensional array is:
(UBOUND (Myarray)-LBOUND (Myarray) + 1) * (UBOUND (Myarray, 2)-LBOUND (Myarray, 2) + 1)
Multi-dimensional array.
4. array Definition
Dim MyArray
MyArray = Array (, 98)
Extensible Array
Dim MyArray()
for i = 0 to 10
ReDim Preserve MyArray(i)
MyArray(i)=i
next
An array that splits a string and returns the split result.
Dim MyArray
MyArray = Split(tempcnt,chr(13)&chr(10))
For I = Lbound(MyArray) to Ubound(MyArray)
Response.Write MyArray(I) & "<br>"
Next
5. array sorting Function
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
Application Example of array sorting Function
Dim MyArray
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
Application.Lock
Application("StoredArray") = MyArray
Application.Unlock
LocalArray = Application("StoredArray")
Overwrite the array in Application
Application.Lock
Application("StoredArray") = LocalArray
Application.Unlock
The Session usage is the same as that of the Application.
7. import data from the database into the Array
This method is often used in function integration of code.
Dim MyArray
Retrieve all records
MyArray = RS. GetRows
Retrieve 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
Convert from: ASP array full set, multi-dimensional array and one-dimensional array-feng-blog Park