Zbex subscripts

From CCARH Wiki
Jump to navigation Jump to search
Previous chapter
Comments
Zbex
Manual
Next Chapter
Program control


Subscripts occur in two contexts: partial strings and array type variables.

All four data types (strings, bit strings, integers, and real numbers) can occur as array type variables. An array may have from one to eight dimensions. The size of each dimension must be stated in the declaration statement. Examples:

    str temp.80(4) One dimension, size = 4. Here we have four string variables, temp(1), temp(2), temp(3) and temp(4), each with maximum length of 80.
    bstr bits.32(2,3) Two dimensions, size1 = 2, size2 = 3. Here we have six (2 x 3) bit string variables, bits(1,1), bits(1,2), bits(1,3), bits(2,1), bits(2,2) and bits(2,3), each with maximum length of 32 bits.
    int t(2,3,2) Three dimensions, size1 = 2, size2 = 3, size3 = 2. Here we have twelve (2 x 3 x 2) integer variables.
    real x(100) One dimension, size = 100. Here we have one hundred real number variables.


When referring to an element of an array variable within a Zbex program, the numbers inside the parentheses (called subscripts) must be integer types, either literals, variables, functions, or some combination of the these (integer expression). Zbex does not allow you to refer to an entire array; neither does it allow you to refer to a single row or a single column in an array. Examples:

        int s(4,6), t(4,6), u(4) 
   
        s = t            (not allowed) 
        s(*,*) = 0       (not allowed) 
        t(*,3) = u(*)    (not allowed) 

In Zbex, you must refer separately to each element in an array. To set all elements of s in the above example to 0 you would write:

        loop for i = 1 to 4 
          loop for j = 1 to 6 
            s(i,j) = 0 
          repeat 
        repeat 

Subscripts can also be used to denote sub-strings within a larger string. In this case, the subscript(s) appear within curly brackets {}. Examples:

        str temp.80 
   
        temp = "For the last time, Mr. Smee, take the princess home!" 
   
        temp{4,11}   /*  Start at position 4 and include 11 characters.  
                     /*     " the last t" 
        temp{4..11}  /*  Include all characters from positions 4 to 11.
                     /*     " the las" 
        temp{11}     /*  character at position 11 
                     /*     "s" 
        temp{11..}   /*  All characters from position 11 to the end 
                          "st time, Mr. Smee, take the princess home!" 

When string subscripts occur in a variable on the left side of an assignment statement, they reference the part of the string that is to be replaced by the right hand side. Example:

        str temp.80 
   
        temp = "For the last time, take the princess home!" 
   
        temp{18} = ", Mr. Smee,"  /* This statement will replace the 
                                  /* comma at position 18 of temp with 
                                  /* the string ", Mr. Smee,".  The 
                                  /* result will be: 

             /* "For the last time, Mr. Smee, take the princess home!"




Previous chapter
Comments
Zbex
Manual
Next Chapter
Program control