Define a simple array
There are two ways to define and initialize arrays in asp, let's see an example of each:
Method one:
MyArray = Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct", "Nov","Dec ")
The size of the array is determined by the number of initialized elements.
Method two:
Copy the code code as follows:
Dim myArray(2) 'Specify the array size
myArray(0)="Jan"
myArray(1)="Feb"
Array dynamic expansion
Copy the code code as follows:
DIM myArray()
REDIM myArray(20) 'Redefine the array size to 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
Example of two-dimensional assignment:
MYArray(3,3)=100
There is also a disguised implementation method for two-dimensional arrays:
dimMyArray(5)
MyArray(0)=Array(...) 'One-dimensional array
MyArray(1)=Array(...)'One-dimensional array
...
When accessing, use the format MyArray(x)(y)
array index
Use the above method to define an array. The subscript of the first element of each dimension array is 0, and the subscript of the last element is the number of elements -1.
But you can also specify the subscript of the array, such as:
dim MyArray1(3 to 10) 'The subscript is 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 is 0.
More applications:
Array sort 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 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
Copy the code code as follows:
DimMyArray
MyArray = Array(1,5,123,12,98)
MyArray = Sort(MyArray)
For I = Lbound(MyArray) to Ubound(MyArray)
Response.Write MyArray(I) & "<br>"
Next
Split a string and return an array
Copy the code code as follows:
DimMyArray
MyArray = Split(string, separator)
For I = Lbound(MyArray) to Ubound(MyArray)
Response.Write MyArray(I) & "<br>"
Next
Using arrays in Application and Session
Application.Lock
Application("StoredArray") = MyArray
Application.Unlock
LocalArray = Application("StoredArray")
Overwrite array in Application
Application.Lock
Application("StoredArray") = LocalArray
Application.Unlock
Session usage is the same as Application
Import data from database into array
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
Pass array to another page
There are many ways to pass an array to another page. There are currently three methods:
Define a comma-separated string, and then use the Split function to re-create the array on the next page.
Store the array in a Session variable and call it on the next page.
Arrays are passed through the hidden area of the form, they are automatically separated by commas, and then the Split function is used to re-create the array.
The first two methods are good, but both are more complicated than the third one. Here we will only introduce the third one 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 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>
What we did above is to use a separate implicit field to store each element in the array 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>
Articles that may interest you: