Arrays:
Array Variables:
Some times you may require assigning more than one related value to a single variable.
Then you can create a variable that can contain a series of values.
This is called an array variable.
Array variables and scalar variables are declared in the same way, except that the declaration of an array variable uses parentheses ( ) following the variable name.
An Array is a contiguous area in the memory referred to by a common name.
Usage:
An array is made up of two parts, the array name and the array subscript.
The subscript indicates the highest index value for the elements within the array.
Each element of an array has a unique identifying index number by which it can be referenced.
VBScript creates zero based arrays where the first element of the array has an index value of zero.
There are 2 types of Arrays available
Single-Dimensional Arrays
Multi-Dimensional Arrays
Single-Dimensional Arrays:
For assigning multiple related values to single variable, Single-Dimensional Arrays will be useful.
In the following example, a single-dimension array containing 11 elements is declared:
Dim A(10)
Although the number shown in the parentheses is 10, all arrays in VBScript are zero-based, so this array actually contains 11 elements.
In a zero-based array, the number of array elements is always the number shown in parentheses plus one.
This kind of array is called a fixed-size array.
You assign data to each of the elements of the array using an index into the array. Beginning at zero and ending at 10, data can be assigned to the elements of an array as follows:
Arr(0) = 256
Arr(1) = 324
Arr(2) = 100
. . .
Arr(9) = 55
Similarly, the data can be retrieved from any element using an index into the particular array element you want. For example:
intVar = Arr(8)
Multi-Dimensional Arrays:
Arrays aren't limited to a single dimension.
You can have as many as 60 dimensions, although most people can't comprehend more than three or four dimensions.
You can declare multiple dimensions by separating an array's size numbers in the parentheses with commas.
In the following example, the MyTeble variable is a two-dimensional array consisting of 6 rows and 11 columns:
Dim MyTable(5, 10)
In a two-dimensional array, the first number is always the number of rows; the second number is the number of columns.
You can also declare an array whose size changes during the time your script is running.
This is called a dynamic array. The array is initially declared within a procedure using either the Dim statement or using the Redim statement.
VBScript provides flexibility for declaring arrays as static or dynamic.
– Static Arrays
– A static array has a specific number of elements.
– The size of a static array cannot be altered at run time.
– Dynamic Arrays
– A dynamic array can be resized at any time.
– Dynamic arrays are useful when size of the array cannot be determined.
– The array size can be changed at run time.
ReDim Statement for Arrays:
ReDim [Preserve] varname(subscripts) [, varname(subscripts)] . . .
For a dynamic array, no size or number of dimensions is placed inside the parentheses.
For example:
Dim MyArray()
To use a dynamic array, you must subsequently use ReDim to determine the number of dimensions and the size of each dimension.
A subsequent ReDim statement resizes the array to 30, but uses the Preserve keyword to preserve the contents of the array as the resizing takes place.
There is no limit to the number of times you can resize a dynamic array, although if you make an array smaller, you lose the data in the eliminated elements.
for a dynamic array, no size or number of dimensions is placed inside the parentheses. For example:
Dim MyArray()
To use a dynamic array, you must subsequently use ReDim to determine the number of dimensions and the size of each dimension.
A subsequent ReDim statement resizes the array to 30, but uses the Preserve keyword to preserve the contents of the array as the resizing takes place.
There is no limit to the number of times you can resize a dynamic array, although if you make an array smaller, you lose the data in the eliminated elements.
To use a dynamic array, you must subsequently use ReDim to determine the number of dimensions and the size of each dimension.
A subsequent ReDim statement resizes the array to 30, but uses the Preserve keyword to preserve the contents of the array as the resizing takes place.
There is no limit to the number of times you can resize a dynamic array, although if you make an array smaller, you lose the data in the eliminated elements.
Preserve Statement for Arrays:
Preserves the data in an existing array when you change the size of the last dimension.
If you use the Preserve keyword, you can resize only the last array dimension, and you can't change the number of dimensions at all.
For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension.
However, if your array has two or more dimensions, you can change the size of only the last dimension and still preserve the contents of the array
Erase Statement for Arrays:
Reinitializes the elements of fixed-size arrays and deallocates dynamic-array storage space.
Erase array
It is important to know whether an array is fixed-size (ordinary) or dynamic because Erase behaves differently depending on the type of array.
Erase recovers no memory for fixed-size arrays. Erase sets the elements of a fixed array as follows:
Fixed numeric array - Sets each element to zero.
Fixed string array - Sets each element to zero-length ("").
Array of objects - Sets each element to the special value Nothing.
Erase frees the memory used by dynamic arrays.
Before your program can refer to the dynamic array again, it must redeclare the array variable's dimensions using a ReDim statement.
Array Function:
Returns a Variant containing an array.
The required arglist argument is a comma-delimited list of values that are assigned to the elements of an array contained with the Variant.
If no arguments are specified, an array of zero length is created.
Example:
Dim A
A = Array(10,20,30)
LBound Function:
LBound(arrayname[, dimension])
Returns the smallest available subscript for the indicated dimension of an array.
The dimension argument means a whole number indicating which dimension's lower bound is returned. Use 1 for the first dimension, 2 for the second, and so on. If dimension is omitted, 1 is assumed
The lower bound for any dimension is always 0.
UBound Function:
UBound(arrayname[, dimension])
Returns the largest available subscript for the indicated dimension of an array.
The dimension argument means a whole number indicating which dimension's lower bound is returned. Use 1 for the first dimension, 2 for the second, and so on. If dimension is omitted, 1 is assumed
The lower bound for any dimension is always 0.
Example:
Dim A(100,3,4)
UBound (A, 1) = 100
UBound (A, 2) = 3
UBound (A, 3) = 4
No comments:
Post a Comment