The book you need to succeed! Vbscript, jscript



Yüklə 12,95 Mb.
Pdf görüntüsü
səhifə21/91
tarix07.11.2018
ölçüsü12,95 Mb.
#78682
1   ...   17   18   19   20   21   22   23   24   ...   91

21
 VBScript 
Essentials 
2
Working with Arrays
Using arrays, you can group related sets of data together. The most common type of array you’ll use 
is one-dimensional, but you can create arrays with up to 60 dimensions if you want to. While a one-
dimensional array is like a column of tabular data, a two-dimensional array is like a spreadsheet 
with rows and columns, and a three-dimensional array is like a 3D grid.
Initializing arrays
Arrays are declared much like regular variables except you follow the variable name with informa-
tion describing the size and dimensions of the array. You can initialize an array with ten data ele-
ments as follows:
Dim bookArray(9)
Values in an array always begin at 0 and end at the number of data points in the array minus 1. This 
is the reason an array with 10 data points is initialized as 
bookArray(9)
. To access elements in an 
array, reference the element’s index position within the array. For example, 
bookArray(0)
 refer-
ences the first element, 
bookArray(1)
 references the second element, and so on. Use the index 
position to set values for the array as well, as in the following:
bookArray(0) = “A Tale Of Two Cities”
bookArray(1) = “Grapes Of Wrath”
Using arrays with multiple dimensions
Multiple dimensions are created by separating the size of each dimension with commas, such as 
currentArray(3,3,3)
 or 
testArray(2,5,5,4)
. You can create a two-dimensional array with 
five columns each with four rows of data points as follows:
Dim myArray(4,3)
Then, if you want to obtain the value of a specific cell in the spreadsheet, you can use the following:
theValue = arrayName(columns -1, rows -1)
in which 
columns
 is the column position of the cell and 
rows
 is the row position of the cell. Following 
this, you can get the value of the cell in column 3, row 2 with this statement:
myValue = myArray(2,1)
Sizing arrays
Sizing arrays on-the-fly allows you to use input from users to drive the size of an array. You declare a 
dynamic array without specifying its dimensions, as follows:
Dim userArray()
86804c02.indd   21
86804c02.indd   21
1/21/09   1:24:29 PM
1/21/09   1:24:29 PM


22
 Part 
I
 
Getting Started with Windows Scripting
Then size the array later using the 
ReDim
 function:
ReDim userArray(currValues - 1)
or
ReDim userArray(numColumns - 1, numRows - 1)
You can also use 
ReDim
 to change the size of an existing array. For example, you can increase the 
size of an array from 10 elements to 20 elements. However, when you change the size of an exist-
ing array, the array’s data contents are destroyed. To prevent this, use the 
Preserve
 keyword, as 
follows:
ReDim Preserve userArray(numColumns - 1, numRows - 1)
VBScript Operators
Operators are used to perform mathematical operations, to make assignments, and to compare val-
ues. The two key types of operators you’ll use in VBScript are arithmetic operators and comparison 
operators. As you’ll see, VBScript supports fewer operators than the command line. While this may 
seem limiting, VBScript makes up for this by allowing you to use floating-point values and integers 
with high precision.
VBScript also has logical operators such as ANDNOTOR, and XOR. With the exception of 
NOT
, these operators are rarely used.
Arithmetic operators
VBScript supports a standard set of arithmetic operators. These operators are summarized in Table 2-5.
TABLE 2-5
Arithmetic Operators in VBScript
Operator
Operation
+
Addition
=
Assignment
/
Division
^
Exponent
Mod
Modulus
*
Multiplication
-
Subtraction/Negation
NOTE
NOTE
86804c02.indd   22
86804c02.indd   22
1/21/09   1:24:29 PM
1/21/09   1:24:29 PM


23
 VBScript 
Essentials 
2
As you can see in Table 2-5, there are few surprises when it comes to VBScript operators. Still, a few 
standouts are worth mentioning. In VBScript, you determine remainders using the 
Mod
 function ver-
sus the 
%
 for the command line. But the syntax is essentially the same. With the expression:
Answer = 9 Mod 3
Answer
 is set to 0. With the expression:
Answer = 9 Mod 2
Answer
 is set to 1.
You can multiply by an exponent with the 
^
 operator. To achieve the same result as 
8 *8 * 8 * 8

you would use:
Answer = 8^4
You can negate a value using the – operator, such as:
Answer = -6 * 2
If you mix operators, VBScript performs calculations using the same precedence order you learned 
in school. For example, multiplication and division in equations are carried out before subtraction 
and addition, which means:
7 + 2 * 2 = 11
and
5 / 5 + 6 = 7
Table 2-6 shows the complete precedence order for operators. As the table shows, exponents have 
the highest precedence order and are always calculated first.
TABLE 2-6
Operator Precedence in VBScript
Order
Operation
1
Exponents (^)
2
Negation (–)
3
Multiplication (*) and Division (/)
4
Remainders (Mod)
5
Addition (+) and Subtraction (-)
86804c02.indd   23
86804c02.indd   23
1/21/09   1:24:29 PM
1/21/09   1:24:29 PM


Yüklə 12,95 Mb.

Dostları ilə paylaş:
1   ...   17   18   19   20   21   22   23   24   ...   91




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə