This article mainly introduces the use of redim and preserve in ASP to create dynamic array instances. This article also gives other array operation examples in ASP. Friends who need it can refer to it.
The function of REDIM in asp is to dynamically define the array length
A statement in a dynamic array can only appear in a procedure and can be used multiple times. You can change the array size and dimensions.
Format:
REDIM [Preserve] array name (subscript 1 [subscript 2....])
Preserve retains the contents of the dynamic array (if not used, all currently stored statements will be lost each time a REDIM statement is executed)
For example:
Copy the code code as follows:Dim DynArray() 'Define the array DynArray() as a dynamic array
REDIM Preserve DynArray(20)'Allocate a number of elements to the array
This is very important for some dynamic changes in arrays in programming, and can often be used and processed. This REDIM has an in-depth understanding of the second computer and thinks it is very helpful to improve one's own programming.
Here are some examples of ASP arrays, of course not all of them are dynamic arrays.
Using arrays in ASP programming:
Definition of array:
Copy the code code as follows:DimMyArray
MyArray = Array(1‚5‚123‚12‚98)
Expandable array:
Copy the code code as follows:DimMyArray()
for i = 0 to 10
ReDim Preserve MyArray(i)
MyArray(i)=i
next
Split a string and return an array of split results:
Copy the code code as follows:DimMyArray
MyArray = Split(tempcnt‚chr(13)&chr(10))
For I = Lbound(MyArray) to Ubound(MyArray)
Response.Write MyArray(I) & <br>
Next
Array sorting function:
Copy the code code as follows: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:
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
Using arrays in Application and Session:
Copy the code code as follows:Application.Lock
Application(StoredArray) = MyArray
Application.Unlock
LocalArray = Application(StoredArray)
Overwrite the array in Application:
Copy the code code as follows:Application.Lock
Application(StoredArray) = LocalArray
Application.Unlock
The usage method of Session is the same as that of Application, importing data from the database into the array:
Copy the code code as follows: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
Through the above examples, we can deepen our understanding of arrays and use them flexibly in practical applications.