Capt. Horatio T.P. Webb
Scripting Declaration Summary
... the names of things ...
&
Arrays

 Data Types    HTML Object References    Array Declarations  

VBSCRIPT

VBscript is a "loosely typed" language. The definition of data items (i.e., variables) does NOT have to be done before using the variable in the body of the program. In contrast to languages like C and COBOL where data items have to be defined (declared) before they are used, VBScript allows you to use variables without having to define them first. The "data type' of the variable is determined by the context of its use.

The choice of a data name is limited to names that do not exceed 255 characters. They should NOT be any VBscript reserve word or function (like loop, for, while, dim, sqr, log, etc.). They should begin with a letter (A-Z or a-z) and may contain the underscore( _ ) character.

Data items are created in VBScript by their usage. All that is required is to assign a value to the name for it to be created (i.e., the data item must appear on the lefty-hand side of the assignment operator (=) ). For example:

fred=1
fred=true
fred="some characters"
fred=1.3E+5

all created variables named fred. These data names may be used to store a variety of different data types. The data types supported by VBScript are:

  1. boolean
    Boolean data is either true or false. These data types are useful for specifying and test conditions where the possible values are either true or false. You can assign a variable the values true or false or use the numeric equivalent. true is numerically equal to -1. false is numerically equal to 0 (zero).

  2. byte
    Byte data can store integers (whole numbers). Byte can contain any value from 0 to 255. This is a numeric data type. (Note that one byte contains 2^8 or 256 values. In hex the two character hex values 00 through ff)

  3. integer
    Used to store integers (whole numbers). Values can range from -32,768 to +32767. This data type uses 2 bytes of memeory. (thus 2^16=65,536. Note that this is the total number of integers between -32,768 and 32,767)

  4. long
    Used to store integers (whole numbers). Values range from -2.147,483,648 to +2,147,683,647. Uses 4 bytes of memory (thus 2^32 =4,294,967,296)

  5. single
    Used to store numbers as floating point (i.e., with a decimal component). Negative numbers range from -1.4 x 10-45 to -3.4 x 1038 and positive numbers range from 1.4 x 10-45 to 3.4 x 1038. These numbers can be represented usig scientific notation where a number like 100,000.0 can be expressed as a power of 10 like 1.0 x 105 is expressed as 1.0E+5.

  6. double
    Same as single, but uses 8 bytes. Negative values can range from -4.9E-324 to 1.8E308. Positive values can range from 4.9E-324 to .8E308.

  7. date (time)
    date is a string used to store dates in mm/dd/yy format
    time is a string used to store time in hh/mm/ss xM format

  8. string
    Used to store a group of characters. The assignment of character strings to a variable of the string type requires that the string be enclosed in quotes (either single ' or double "").

    See the sequentials page here for converting types of numbers and string in vbscript.

  9. object
    Used to reference objects.

  10. error
    Used to store error types.

  11. empty
    Used to specify a variable that has been created. For numeric variables are assigned a value of 0 and strings are assigned a value of "".

  12. null
    Used to represent variables that have not been initially assigned values.

JAVASCRIPT

Javascript is also a "loosely typed" language. The definition of data items (i.e., variables) does NOT have to be done before using the variable in the body of the program. The "data type' of the variable is determined by the context of its use. Variable names must begin with a letter or underscore (i.e, a thru Z, a thru z or _). Following the first character they can contain any letter, number or the underscore. Names cannot contain any punctuation character (e.g., , ( ) ; ' " $ %, etc.) and cannot be any javascript reserved words (e.g., for, Array, new, true, false, if, etc.).

  1. Numeric Values

    Two basic numeric data types exist in javascript:

    1. Integers

      These are whole numbers without a decimal point. They preceeded by a negative sign or a positive sign. The range of values is from -253 to 253. You may also use integers expressed as hexadecimal digits (base 16) by preceeding the number with "0x" (that is zero, then "x"). Also octal integers may be expressed by preceeding the number with a 0 (zero). Thus:
      x=21
      x=0x15
      x=025

      all represent the integer 21.

    2. Floating Point or Real Numbers

      These numbers have decimal point and at least one digit. They preceeded by a negative sign or a positive sign. You may also use exponential notaion -- that is using a number with a decimal point followed by the letter "e" or "E" and a power of 10. Thus

      x=142.7
      x=1.427e2
      x=0.1427e3

      all represent the floating point number 142.7.

  2. Strings

    String are one or more characters enclosed in quotes (either single quotes are double quotes). The following are valid strings

    "ABC"
    'ABC'
    "12345"
    "Forbe's"

Data types are defined by their context -- not specific declaractions (i.e., when making a variable assignment, VBScript and Javascript both choose the most appropriate data type for the given values.
The Names of Objects

In addition to data items created by the program, there are objects that are used on the HTML page (e.g., radio buttons, cjheck boxes, etc.) that may be referenced by the VBScript code. These "intrinsic" (i.e., built in) HTML objects cannot be referenced by the use of the name= value in the HTML definition. They must be located in their context of the HTML code that is currently in the window. To reference any HTML object in the VBScript code, you can first establish a name for the object by using the SET command. The format is:

set name_of_object = object hierarchy

In javascript the same definition is applied by using:

var name_of_object = object hierarchy;

the object hierarchy is defined by the location of the object relative to the current window's contents. The entire browser window is called window. The window contains an HTML document named document, the document may contain one or more forms and the form may contain one or more intrinsic HTML objects (elements). the general format is:

window . document . forms(number_of_the_form)
or
name_of_the_form
. elements(number_of_the_element)
or
name_of_the_element
. property
or
method

Note that the form (name or number) and the element (name or number) can be used interchangeably.

For example, suppose the following HTML page is to be referenced by VBScript code:

<html><body>
<form name="formone">
<input type="text" name="T1"><br>
<input type="text" name="T2"><br>
<input type="text" name="T3"><br>
</form>
<form name="formtwo">
<input type="text" name="T4"><br>
<input type="text" name="T5"><br>
<input type="text" name="T6"><br>
</form>
</body><html>

Then the six items' values can be referenced as:

  1. T1
    window.document.forms(0).elements(0).value OR
    window.document.formone.elements(0).value OR
    window.document.forms(0).T1.value OR
    window.document.formone.T1.value OR
  2. T2
    window.document.forms(0).elements(1).value OR
    window.document.formone.elements(1).value OR
    window.document.forms(0).T2.value OR
    window.document.formone.T2.value OR
  3. T3
    window.document.forms(0).elements(2).value OR
    window.document.formone.elements(2).value OR
    window.document.forms(0).T3.value OR
    window.document.formone.T3.value OR
  4. T4
    window.document.forms(1).elements(0).value OR
    window.document.formtwo.elements(0).value OR
    window.document.forms(1).T4.value OR
    window.document.formtwo.T4.value OR
  5. T5
    window.document.forms(1).elements(1).value OR
    window.document.formtwo.elements(1).value OR
    window.document.forms(1).T5.value OR
    window.document.formtwo.T5.value OR
  6. T6
    window.document.forms(1).elements(2).value OR
    window.document.formtwo.elements(2).value OR
    window.document.forms(1).T6.value OR
    window.document.formtwo.T6.value OR

    Note that the elements and forms BOTH begin numbering the items at zero. The SET verb can be used to create shorthand names for the objects. To create a shorthand name for the object use:

    SET short_name = window.document.form name or forms(number).element name or elements (number)

    Note that the property, method or event CANNOT be specified with SET. You can only create the shorthand_name by specifying down to the element level. Using the example above, a shorthand name for T1 could be formed by using:

    SET st1=window.document.forms(0).elements(0)

    Now the object named st1 is the object T1 defined in the HTML. You may use any property or method for st1 (i.e., T1) in the code by saying for example:

    st1.value (value of T1, same as window.document.forms(0).elements(0).value)
    st2.name (returns the bvalue T1,same as window.document.forms(0).elements(0).name)

    See the HTML Object Model for more details.

VBSCRIPT ARRAYS

Arrays can be called vectors, lists or tables. They are used to store multiple items with the same data name -- but refer to the items by their number (where they appear in the list) rather than by name (i.e., where they appear in the array). Arrays come in two forms: (1) static and (2) dynamic.

  1. Static arrays are defined by a DIM statement that specifies how many items are to stored in the array (list or vector). Usage is:

    DIM <name_of_the_array> (number-of_items)

    Since array items are numbered beginning at zero, to specify n items named fred you would use DIM fred (n). For example, to specify an array of 10 elements named fred, you would say:

    DIM fred(10)

    The items can then be referenced by:

    fred(0) is the first item in the array
    fred(1) is the second item in the array
    fred(2) is the third item in the array
    fred(3) is the fourth item in the array .
    .
    .
    fred(9) is the tenth (last) item in the array

    You can also initiliza an array by using the Array function. NOTE the "DIM" is omitted.

    <name_of_the_array> = Array ( 1st value , 2nd value , ... )

    For example:

    fred= Array ("TX","AR","NY","CA")

    This initializes fred so that: fred(0)="TX", fred(1)="AR", fred(2)="NY" and fred(3)="CA"

    Much shorter!

  2. Dynamic arrays are defined without specifying the exact number of array elements., They form is:

    DIM name_of_array ()

    You can then use the command:

    REDIM name_of_array (number_of_items )

    to specify the array size. This allows you to size the array inside the program. You must use REDIM before assigning values to any items in the array. If you use a REDIM command, the original array is destroyed (if any) and a new one created. To avoid this you can include the keyword PRESERVE which causes the original array values to be retained. The usage of preserve is:

    REDIM PRESERVE name_of_array ( new_size_of_array)

    For example, if you started with:

    DIM fred(5)

    and then wished to retain the current values AND make the array size 10, then you would use:

    REDIM PRESERVE fred(10)

To test the current size of the array you can use the Ubound function. The format is:

size_of_array = UBound ( name_of_the_array)

This will return the current array size. Note that this is the maximum allowable value of a subscript -- NOT the count of array elements.

You can also erase the array from memory by using the Erase command. The format is:

Erase name_of_the_array

Multi-Dimensional arrays can also be specified. For two-dimensional arrays (called matrices or tables -- as opposed to vectors and lists in the one dimensional case), you must specify the number of rows and the number of columns the array is to contain. Usage is:

DIM name_of_the_array ( Number_of_rows , number_of_columns )

for example an array named fred with 3 rows and 5 columns would be defined by:

DIM fred(3,5)

You may also specify three dimensional arrays using:

DIM name_of_the_array ( Number_of_rows , number_of_columns , number_of_planes )

In this three dimensional case the third subscript refers to planes. For example:

DIM fred(3,5,6)

creates an array with three rows, five columns and six planes.

JAVASCRIPT ARRAYS

Javascript arrays are defined in one of five ways:

1. var name_of_the_array= new Array();

2. var name_of_the_array= new Array(size_of_array);

3. var name_of_the_array = [];

4. var name_of_the_array= new Array(element_value0,element_value1,element_value2,...element_valuen);

5. var name_of_the_array= [element_value0,element_value1,element_value2,...element_valuen];

Note that items (4) and (5) above allow you to define AND initialize the array in a single statement.

Note: Javascript uses square brackets (e.g., x[i] )
rather than the parentheses in vbscript (e.g., x(i) ).

The elements of the array are numbered starting at zero. If the size option is used, then the last element of the array is size-1. If the elements are specifed (as in version 3), the number of elements is n+1 (i.e., 0,1,2,...,n).

Elements in the array are referenced by an integer subscript such as:

    name_of_array[subscript]

The number of elements in the array may be accessed by:

    name_of_array.length

Array methods and functions:

  1. concat (concatenation)

        name_of_array_1.concat(name_of_array_2)

    returns an new array with the elements of name_of_array_1 appended to the elements of name_of_array_2. The resulting length of the new array is the sum of the name_of_array_1 and name_of_array_2 lengths.

  2. reverse

        name_of_array.reverse()

    causes the elements of the array to have there order reversed.

  3. slice

        name_of_array.slice(starting_element [ending element]);

    Returns an array that is a copy of a portion of name_of_the_array. The copy begins at starting_element and copies all elements up to BUT NOT INCLUDING ending_element if it is specified. If ending element is not specified, the entire array from start_element to the end is copied. If ending_element is negative, this indicates how many elements from the end of the array to stop (e.g., if endinbg_element is -2, all but the last two elements are copied)

  4. sort

        name_of_array.sort();

    Sorts the array in ascending ascii order. No new array is produced.

  5. join

        name_of_array.join (delemiter);

    The array must contain strings. The mjoin returns a string by concatenating the string elements of the array together and separated by the delimeter. If delimiter is omitted, the strings are are concatenated with NO space between the strings.

Two dimensional arrays are created in Javascript by first creating an array and then making each element of the array a new array. the general form for an array with n rows and m columns would be:

var array_name = new Array (n-1);
array_name[0]=new Array (m-1);
array_name[1]=new Array (m-1);
array_name[2]=new Array (m-1);
.
.
.
array_name[n-1] = new Array (m-1);

A reference to any row/column element is then:

array_name[row_index][column_index]

One can do this more quickly in the following loop:

var array_name = new Array (n-1)
for (i=0; i < n-1; i++)
  array_name[i] = new Array (m-1)


Note:
Javascript uses square bracket pairs for each subscript -- e.g.,
   x[i][j]
Vbscript uses single parentheses separating the subscripts by commas -- e.g.,
   x(i,j)
 
        . vbscript array example
        . a faster vbscript array example
        . a faster vbscript array example with a timer
 
 
        . javascript array example
        . a faster javascript array example
        . a faster javascript array example with a timer