ASP Dynamic Array

Source: Internet
Author: User

The ASP array mentioned in this Article refers to an array in ASP that uses the default language VBScript as the language.

Example:

Dim myarray ()
For I = 0 to 10
Redim preserve myarray (I)
Myarray (I) = I
Next

Subscript

  • The subscript of the ASP array also starts from 0.
  • To obtain the value of an element, enclose the array variable with parentheses (for example, arr (0). Note that it is not a square brackets but a parentheses.

Definition

Dim Arr () 'defines a dynamic array

Dim arr2 (2) 'defines a static Array

The variable name is enclosed by parentheses to form an array. If a value is specified in brackets, a static array is defined, that is, an array of fixed sizes.

Code

  • Arr () defines a dynamic array and must be redefined to determine the length before it can be used. For more information about redefinition, see.
  • Arr2 (2) defines the maximum number of subscripts
    2 (3 in length) Static array, which is different from other languages (such as C # and JavaScript). Parameters in brackets of other languages indicate the length.

Erase

The storage space of the dynamic array is released when the erase dynamic array is used. When the erase static array is used, only the element value of the array is initialized.

Dim Arr ()

Redim Arr (2)

Erase arr

Alert (ubound (ARR) 'error, erase
All buckets of the dynamic array have been released.


Dim Arr (1)

Erase arr

Alert (ubound (ARR) 'display
1. Erase only initializes the element value of the static array, and the Occupied Space is still there.

Redim redefinition

For dynamic arrays, You can redefine them. You can also re-define them. Static arrays cannot be redefined.

Dim Arr ()

Dim arr2 (2)

Dim arr3 ()

Redim Arr (0) 'sets the maximum subscript of array arr to 0 (Length: 1 ).

Redim arr2 (1) 'error.

Redim arr3 (-1) 'is correct and can be set
-1 indicates that the length of this array is 0.

If you redefine an array, the original value of the array will be lost.

Redim preserve

As mentioned above, redefining an array will lead to the loss of the original value assignment of this array. How can this problem be ensured? Use redim preserve.

Dim Arr ()

 

Redim Arr (1)

Arr (0) = "1"

Alert (ARR (0) 'shows 1

 

Redim preserve Arr (2)

Alert (ARR (0) 'still displays 1

Ubound

ASP
The array does not have an attribute or method for obtaining the length. Only the method for obtaining the maximum subscript-ubound (array name), that is, the maximum value of the array subject, such as ubound (ARR ).

Dim Arr (1)

Response. Write (ubound (ARR) '. The upper limit is 1 and the array length is 2.


Dim arr2 ()

Redim arr2 (-1) 'can be a dynamic array redim up to-1, but cannot be other negative numbers. For example, if-2 is used, it will report "insufficient memory ".

Response. Write (ubound (arr2) 'display upper limit-1, array length is 0


Dim arr3 ()

'Response. Write (ubound (arr3) 'Error

Split

Split splits strings into arrays. The following arr2 is incorrect.

Dim arr

Dim arr2 () 'error. brackets are not allowed here.

Arr = Split ("1, 2, 3 ",",")

Arr2 = Split ("1, 2, 3", ",") dim myarray ()
For I = 0 to 10
Redim preserve myarray (I)
Myarray (I) = I
Next example: dim myarray ()
For I = 0 to 10
Redim preserve myarray (I)
Myarray (I) = I
Next example:

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.