Define a simple array
There are two ways to define and initialize arrays in asp. Let's take a look at each example:
Method 1:
MyArray = Array ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep ", "Oct", "Nov", "Dec ")
The array size is determined by the number of initialization elements.
Method 2:
Copy codeThe Code is as follows: Dim myArray (2) 'specifies the array size
MyArray (0) = "Jan"
MyArray (1) = "Feb"
Array dynamic expansion
Copy codeThe Code is as follows: DIM myArray ()
REDIM myArray (20) 'redefines the array size as 20
ReDim Preserve MyArray (I) 'Preserve retains the original data in the array
Two-dimensional array
Example:
Dim MyArray (5, 10) 'defines a two-dimensional array
Two-dimensional assignment example:
MYArray (3,3) = 100
There is also a disguised Implementation Method for two-dimensional arrays:
Dim MyArray (5)
MyArray (0) = Array (...) 'One-dimensional Array
MyArray (1) = Array (...) 'One-dimensional Array
...
During access, the MyArray (x) (y) format is used.
Subscript of the array
The above method is used to define an array. The subscript of the first element in each one-dimensional array is 0, and the subscript of the last element is the number of elements-1.
However, you can also specify the subscript of the array, for example:
Dim MyArray1 (3 to 10) 'subscript from 3 to 10. MyArray (3) gets the value of the first element.
Useful array Functions
Ubound (array name) function -- returns the subscript of the last element of the array.
Lbound (array name) function -- returns the subscript of the first element of the array. The default value is 0.
More applications:
Array sorting Function
[Code]
Function Sort (ary)
KeepChecking = TRUE
Do Until KeepChecking = FALSE
KeepChecking = FALSE
For I = 0 to UBound (ary)
If I = UBound (ary) Then Exit
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
Copy codeThe Code is as follows: Dim MyArray
MyArray = Array (, 98)
MyArray = Sort (MyArray)
For I = Lbound (MyArray) to Ubound (MyArray)
Response. Write MyArray (I) & "<br>"
Next
Splits a string and returns an array.
Copy codeThe Code is as follows: Dim MyArray
MyArray = Split (string, delimiter)
For I = Lbound (MyArray) to Ubound (MyArray)
Response. Write MyArray (I) & "<br>"
Next
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.
Import data from the database into the Array
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
Pass an array to another page
There are currently many ways to pass arrays to another page. There are currently three methods:
Define a comma-separated string, and then use the Split function on the next page to recreate the array.
Store the array in a Session variable and call it on the next page.
The hidden areas of the form are used to pass the array. They are all automatically separated by commas, and then re-create the Array Using the Split function.
The first two methods are good, but they are more complicated than the third method. Here we will only introduce the third type, because it is the simplest and most effective.
1. asp:
<%
Dim I
Dim myArray (20)
For I = 0 to 20
MyArray (I) = "Item" & I
Next
%>
<Html>
<Body>
<Form name = "testform" method = "post" action = "2.asp">
<%
For I = 0 to ubound (myArray)
Response. write "<input type = hidden name = myArray value = '" & myArray (I) & "'>"
Next
%>
<P>
<Input type = "submit">
</Form>
</Body>
</Html>
The above is to store each element in the array with a separate hidden field in a form. Let's look at the next page:
2. asp
<Html>
<Body>
<%
Dim arrString
Dim myArray
Dim I
ArrString = request ("myArray ")
MyArray = split (arrString ,",")
For I = 0 to ubound (myArray)
Response. write "Item" & I & "=" & myArray (I) & "<br>" & vbCrLf
Next
%>
</Body>
</Html>