Using ReDim and Preserve in ASP to create dynamic array instances _ Application Tips

Source: Internet
Author: User
Tags array length array sort arrays chr

The function of ReDim in ASP is to define the array length dynamically
  
A statement inside a dynamic array can only appear in the process and may be used more than once. You can change the size of the array, and the number of dimensions.

Format:

REDIM [Preserve] Array name (subscript 1[subscript 2 ...] )
Preserve retains the contents of the dynamic array (if not, the currently stored statement is lost every time the ReDim statement is executed)

For example:

Copy Code code as follows:

Dim Dynarray () ' defines an array dynarray () as a dynamic array
REDIM Preserve Dynarray (20) ' assigns a number of tuples to the array

This is very important for some of the dynamic array changes in programming, and it is often used, processed, and ReDim to understand that the second computer is helpful in improving its programming.

Here are some examples of ASP arrays, of course not all dynamic arrays

To use arrays in ASP programming:

The definition of an array:

Copy Code code as follows:

Dim myarray
MyArray = Array (1 ' 5 ' 123 ' 12 ' 98)

Extensible array:

Copy Code code as follows:

Dim MyArray ()
For i = 0 to 10
ReDim Preserve myarray (i)
MyArray (i) =i
Next

Splits a string and returns an array of split results:

Copy Code code as follows:

Dim myarray
MyArray = Split (tempcnt ' Chr) &CHR (10))
For I = Lbound (myarray) to Ubound (myarray)
Response.Write MyArray (I) & "<br>"
Next

Array sort function:

Copy 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

An example of an array sort function application:

Copy Code code as follows:

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

To use arrays in application and sessions:

Copy Code code as follows:

Application.Lock
Application ("StoredArray") = MyArray
Application.UnLock
LocalArray = Application ("StoredArray")

To overwrite an array in application:

Copy Code code as follows:

Application.Lock
Application ("StoredArray") = LocalArray
Application.UnLock

The session is used in the same way as application to import data from the database into an array:

Copy Code code as follows:

Dim myarray
' Take out all records
MyArray = RS. GetRows
' Take out the top 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 the logarithmic group and use it flexibly in practical application.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.