VBScript
VBScript
Session 2
What we learn last session?
What we learn last session?
Data types. Data types.
Declaring, assigning and usage of variables. Declaring, assigning and usage of variables. Option Explicit
Option Explicit statement.statement. VBScript keywords (Null,True
VBScript keywords (Null,True ……))
Scope and liftime of variables. Scope and liftime of variables. Rem
Subjets for Session 2
Subjets for Session 2
Scalar variables and array variables. Scalar variables and array variables. Redim statement. Redim statement. Preserve statement. Preserve statement. Erase statement. Erase statement. Array function. Array function.
LBound and UBound functions. LBound and UBound functions.
Scalar Variables and Array
Scalar Variables and Array
Variables
Variables
Much of the time, you only want to assign a single value Much of the time, you only want to assign a single value to a variable you have declared.
to a variable you have declared.
A variable containing a single value is a scalar variable. A variable containing a single value is a scalar variable. Other times, it is convenient to assign more than one Other times, it is convenient to assign more than one related value to a single variable.
related value to a single variable.
Then you can create a variable that can contain a series Then you can create a variable that can contain a series of values.
of values.
This is called an array variable. This is called an array variable.
Array variables and scalar variables are declared in the Array variables and scalar variables are declared in the same way, except that the declaration of an array
same way, except that the declaration of an array variable uses parentheses ( ) following the variable variable uses parentheses ( ) following the variable name.
Scalar Variables and Array
Scalar Variables and Array
Variables
Variables
In the following example, a single-dimension array In the following example, a single-dimension array containing 11 elements is declared:
containing 11 elements is declared: Dim
Dim A(10)A(10)
Although the number shown in the parentheses is 10, all Although the number shown in the parentheses is 10, all arrays in VBScript are zero-based, so this array actually arrays in VBScript are zero-based, so this array actually contains 11 elements.
contains 11 elements.
In a zero-based array, the number of array elements is In a zero-based array, the number of array elements is always the number shown in parentheses plus one.
always the number shown in parentheses plus one. This kind of array is called a fixed-size array.
Scalar Variables and Array
Scalar Variables and Array
Variables
Variables
You assign data to each of the elements of the array
You assign data to each of the elements of the array using anusing an index into the array. Beginning at zero
index into the array. Beginning at zero and ending at 10, data canand ending at 10, data can be assigned to the elements of an array
be assigned to the elements of an array as follows:as follows:
A(0) = 256 A(0) = 256 A(1) = 324 A(1) = 324 A(2) = 100 A(2) = 100 . . . . . . A(10) = 55 A(10) = 55
Similarly, the data can be retrieved from any element Similarly, the data can be retrieved from any element using an index into the particular array
using an index into the particular array element you want.element you want. For example:
For example:
SomeVariable = A(8) SomeVariable = A(8)
Scalar Variables and Array
Scalar Variables and Array
Variables
Variables
Arrays aren't limited to a single dimension. Arrays aren't limited to a single dimension.
You can have as many as 60 dimensions, although most people You can have as many as 60 dimensions, although most people can't comprehend more than three or four dimensions.
can't comprehend more than three or four dimensions. You can declare multiple dimensions by separating an
You can declare multiple dimensions by separating an array's sizearray's size numbers in the parentheses with
numbers in the parentheses with commas.commas.
In the following example, the MyTeble variable
In the following example, the MyTeble variable is a two-is a two-dimensional array consisting of 6 rows and 11 columns: dimensional array consisting of 6 rows and 11 columns:
Dim
Scalar Variables and Array
Scalar Variables and Array
Variables
Variables
In a two-dimensional array, the first number is always the In a two-dimensional array, the first number is always the number of rows; the second number is the number of
number of rows; the second number is the number of columns.
columns.
You can also declare an array whose size changes during You can also declare an array whose size changes during the time your script is running.
the time your script is running.
This is called a dynamic array. The array is initially declared This is called a dynamic array. The array is initially declared within a procedure using either the
within a procedure using either the DimDim statement or usingstatement or using the
Arrays
Arrays
Redim Statement
Redim Statement
ReDim
ReDim [[PreservePreserve] varname(] varname(subscriptssubscripts)) [[,, varnamevarname((subscriptssubscripts))]] . . .. . .
for a dynamic array, no size or number of dimensions is placed for a dynamic array, no size or number of dimensions is placed inside the parentheses. For example:
inside the parentheses. For example: Dim
Dim MyArray()MyArray()
To use a dynamic array, you must subsequently use
To use a dynamic array, you must subsequently use ReDimReDim toto determine the number of dimensions and the size
determine the number of dimensions and the size of eachof each dimension.
dimension. A subsequent
A subsequent ReDimReDim statement resizes the array to 30, but usesstatement resizes the array to 30, but uses the
the PreservePreserve keyword to preserve the contents of the array as thekeyword to preserve the contents of the array as the resizing takes place.
resizing takes place.
There is no limit to the number of times you can resize a dynamic 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 array, although if you make an array smaller, you lose the data in the eliminated elements.
Arrays
Arrays
Redim Statement
Redim Statement
for a dynamic array, no size or number of dimensions is placed for a dynamic array, no size or number of dimensions is placed inside the parentheses. For example:
inside the parentheses. For example:
Dim
Dim MyArray()MyArray()
To use a dynamic array, you must subsequently useTo use a dynamic array, you must subsequently use ReDimReDim toto
determine the number of dimensions and the size
determine the number of dimensions and the size of eachof each dimension.
dimension.
A subsequentA subsequent ReDimReDim statement resizes the array to 30, but usesstatement resizes the array to 30, but uses
the
the PreservePreserve keyword to preserve the contents of the array as thekeyword to preserve the contents of the array as the resizing takes place.
resizing takes place.
There is no limit to the number of times you can resize a dynamicThere 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 array, although if you make an array smaller, you lose the data in the eliminated elements.
Preserve Statement
Preserve Statement
Preserves the data in an existing array when you change the size of the last Preserves the data in an existing array when you change the size of the last dimension.
dimension. If you use the
If you use the PreservePreserve keyword, you can resize only the last arraykeyword, you can resize only the last array dimension, and you can't change the number of dimensions at all. 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 For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension.
dimension because it is the last and only dimension.
However, if your array has two or more dimensions, you can change the 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 size of only the last dimension and still preserve the contents of the array
ReDim ReDim X(10, 10, 10)X(10, 10, 10) . . . . . . ReDim Preserve ReDim Preserve X(10, 10, 15)X(10, 10, 15) Caution Caution
If you make an array smaller than it was originally, If you make an array smaller than it was originally, data in the eliminated elements is lost.
Memory
Memory
(Dim a(5 (Dim a(5 (Redim a(6 (Redim a(6(Redim preserve a(8 (Redim preserve a(8
Erase Statement
Erase Statement
Reinitializes the elements of fixed-size arrays
Reinitializes the elements of fixed-size arrays and deallocatesand deallocates dynamic-array storage space.
dynamic-array storage space. Erase
Erase arrayarray
It is important to know whether an array is f
It is important to know whether an array is fixed-size (ordinary) orixed-size (ordinary) or dynamic because
dynamic because EraseErase behaves differently depending on the typebehaves differently depending on the type of array.
of array. Erase
Erase recovers no memory for fixed-size arrays.recovers no memory for fixed-size arrays. EraseErase sets thesets the elements of a fixed array as follows:
elements of a fixed array as follows:
Fixed numeric array - Sets each element to zero. Fixed numeric array - Sets each element to zero. Fixed string array - S
Fixed string array - Sets each element to zero-length ("").ets each element to zero-length (""). Array of o
Array of objects - bjects - Sets each Sets each element to element to the special vthe special valuealue Nothing.
Erase Statement
Erase Statement
Erase
Erase frees the memory used by dynamic arrays.frees the memory used by dynamic arrays.
Before your program can refer to the dynamic array again, Before your program can refer to the dynamic array again, it must redeclare the array variable's dimensions using a it must redeclare the array variable's dimensions using a ReDim
Array Function
Array Function
Returns a
Returns a VariantVariant containing an array.containing an array. The required
The required arglist arglist argument is a comma-delimited list of argument is a comma-delimited list of values that are assigned to the elements of an array
values that are assigned to the elements of an array contained with the
contained with the VariantVariant..
If no arguments are specified, an array of zero length is If no arguments are specified, an array of zero length is created. created. Dim Dim AA A = A = ArrayArray((1010,,2020,,3030))
LBound Function
LBound Function
LBound
LBound((arrayname[arrayname[,, dimension]dimension]))
Returns the smallest available subscript for the indicated Returns the smallest available subscript for the indicated dimension of an array.
dimension of an array. The dimension
The dimension argument means a whole number indicatingargument means a whole number indicating which dimension's lower bound is returned. Use 1 for the which dimension's lower bound is returned. Use 1 for the first dimension, 2 for the second, and so on. If
first dimension, 2 for the second, and so on. If dimensiondimension isis omitted, 1 is assumed
omitted, 1 is assumed
The lower bound for any dimension is always 0. The lower bound for any dimension is always 0.
UBound Function
UBound Function
UBound
UBound((arrayname[arrayname[,, dimension]dimension]))
Returns the largest available subscript for the indicated Returns the largest available subscript for the indicated dimension of an array.
dimension of an array. The dimension
The dimension argument means a whole number indicatingargument means a whole number indicating which dimension's lower bound is returned. Use 1 for the which dimension's lower bound is returned. Use 1 for the first dimension, 2 for the second, and so on. If
first dimension, 2 for the second, and so on. If dimensiondimension isis omitted, 1 is assumed
omitted, 1 is assumed
The lower bound for any dimension is always 0. The lower bound for any dimension is always 0.
Dim
Dim ( A(100,3,4( A(100,3,4 UBound
UBound (A, 1) = 100(A, 1) = 100 UBound
UBound (A, 2) = 3(A, 2) = 3 UBound
Lab 2.1
Lab 2.1
Lab 2.1
Lab 2.1
Write a small program Write a small program
Declare using
Declare using DimDim 2 array variables.2 array variables.
arr1
arr1 – – size of 5size of 5
arr2
arr2 – – no sizeno size
Assign descending values to arr1 (i.e.
Assign descending values to arr1 (i.e. 5,4,3,2..0)5,4,3,2..0)
Resize the first array to 10, no data loss Resize the first array to 10, no data loss Resize the second array to 5.
Resize the second array to 5.
Print the content of the arrays to the reporter. Print the content of the arrays to the reporter.