SheerPower® 4GL
A Guide to the SheerPower Language


Previous Contents Index

EXAMPLE:

Example 5-17 Private variables in routines

  do_totals 
  end
 
  routine do_totals: private mytotal, desc$ 
    mytotal = 15 
    desc$ = 'Test Totals' 
    print desc$; mytotal 
  end routine
 
 
Test Totals 15 

DESCRIPTION

SheerPower allows you to use private variables in a routine. Private variables are variables identified with a specific routine. This option allows you to use the same variable names more than once because, internally, SheerPower prefixes the variables with the routine name and a "$" character.

In the above example, the private variables "mytotal" and "desc$" are internally known to SheerPower as:

do_totals$mytotal

do_totals$desc$

From inside the routine, you can reference the variables by their private names. For example: mytotal, desc$

From outside the routine (or while debugging the code), you can reference the private variables by their internal known names. For example: do_totals$mytotal, do_totals$desc$

Note

See Appendix M, SheerPower and Program Segmentation for more on routines and private routines in SheerPower.

5.8 Defining arrays

Arrays are a type of variable. They are used to store and manipulate tables of variable information. An array must be defined before it is used in a program. Array variables are described in Section 4.5.1, Arrays.

5.8.1 Dimensioning Arrays

Arrays are dimensioned with a DIM statement. The REDIM statement can be used to redimension an array; that is, to change the dimensions of an array which has been defined with the DIM statement. The OPTION BASE statement changes the default low bound. By default, the low bound is 1.

About arrays:

5.8.2 DIM

FORMAT:


        DIM [INTEGER | REAL | STRING | BOOLEAN] 
                  array_name ([int_expr TO] int_expr [, ...]) 

EXAMPLE:

Example 5-18 DIM statement

  dim name$(4) 
  for i = 1 to 4 
    input 'Enter a name': name$(i) 
  next i 
  print
  for i = 1 to 4 
    print i; ' '; name$(i) 
  next i 
  end
 
 
Enter a name? Jim 
Enter a name? Jane 
Enter a name? Bob 
Enter a name? Betty 
 
1  Jim 
2  Jane 
3  Bob 
4  Betty 

PURPOSE:

DIM is used to dimension arrays. Arrays are used to store tables of variable information. An array must be dimensioned before it can be used.

DESCRIPTION:

The simplest version of a DIM statement is:


        DIM array_name(int_expr) 

array_name is the name of the array being defined. The array name must meet the rules for variable names. int_expr is the high bound for the array---the highest element allowed in a dimension. The low bound is the lowest element allowed in a dimension. The low bound defaults to 1. For example:


        DIM NAME$(4) 

This statement defines a one-dimensional array with four elements:


NAME$(1) 
NAME$(2) 
NAME$(3) 
NAME$(4) 

Multiple Dimensions

An array can have up to 32 dimensions. A high bound must be specified for each dimension.


        DIM array_name(int_expr [, int_expr, ...]) 

For example:


          dim name$(4,2) 

This statement defines the following two-dimensional array:


NAME$(1,1)   NAME$(1,2) 
NAME$(2,1)   NAME$(2,2) 
NAME$(3,1)   NAME$(3,2) 
NAME$(4,1)   NAME$(4,2) 

Low Bounds

The low bound is the lowest element a dimension can have. Low bounds can be specified for each dimension of an array. If no low bound is specified, the default is 1. To specify a low bound, use the following format:


        DIM array_name (int_ expr TO int_expr) 

The number preceding TO is the low bound. For example:


          dim name$(4,18 to 20) 

This statement creates an array whose first dimension contains elements 1-4 and whose second dimension contains elements 18-20:


NAME$(1,18)   NAME$(1,19)   NAME$(1,20)   
NAME$(2,18)   NAME$(2,19)   NAME$(2,20)   
NAME$(3,18)   NAME$(3,19)   NAME$(3,20)   
NAME$(4,18)   NAME$(4,19)   NAME$(4,20)   

5.8.3 REDIM

FORMAT:


        REDIM array_name (int_expr, int_expr...) ... 
 
             OR 
 
        REDIM array_name [( [int_expr TO] int_expr, 
                      [int_expr TO] int_expr... )] ... 

EXAMPLE:

Example 5-19 REDIM statement

  dim name$(2) 
  input 'How many names': num 
  redim name$(num) 
  for i = 1 to num 
    input 'Enter a name': name$(i) 
  next i 
  do
    print
    for i = 1 to num 
    if  name$(i) = ''  then
      print i; ' '; 'empty slot' 
    else
      print i; ' '; name$(i) 
    end if
    next i 
    print
    input 'How many names': num 
    if  _back or _exit  then exit do
    redim name$(num) 
  loop
  end
 
 
How many names? 3 
Enter a name? Tim 
Enter a name? Sammy 
Enter a name? Fred 
 
1  Tim 
2  Sammy 
3  Fred 
 
How many names? 4 
 
1  Tim 
2  Sammy 
3  Fred 
4  empty slot 
 
How many names? exit 

PURPOSE:

The REDIM statement is used to change the size of an array.

DESCRIPTION:

REDIM redimensions arrays. REDIM can be used only on arrays that have already been dimensioned with the DIM statement. The REDIM statement has the same rules, options and limits as the DIM statement.

Arrays can be dynamically expanded as needed. If you REDIM a single dimension array or the first dimension of a multi-dimensioned array to a larger size, the old values are kept. If you REDIM any array to a smaller size or REDIM two or more dimensions in a multi-dimensioned array to a larger size, the old values are lost.

If your application depends on REDIM initializing all array values, change your code as follows:


        Old Code:       REDIM X(100) 
 
        New Code:       REDIM X(1) 
                        REDIM X(100) 

The REDIM X(1) forces all array values to be initialized by the second REDIM statement.

5.8.4 OPTION BASE

FORMAT:


        OPTION BASE [0 | 1] 

EXAMPLE:

Example 5-20 OPTION BASE statement

  option base 0 
  dim name$(4) 
  for i = 0 to 4 
    input 'Enter a name': name$(i) 
    print i; ' Hello, '; name$(i) 
  next i 
  end
 
 
Enter a name? June 
 0  Hello, June 
Enter a name? Tony 
 1  Hello, Tony 
Enter a name? Sandy 
 2  Hello, Sandy 
Enter a name? Carl 
 3  Hello, Carl 
Enter a name? Liz 
 4  Hello, Liz 

PURPOSE:

OPTION BASE is used to set the default low bound for arrays to suit your needs. You have the option of starting the array with element O or element 1.

DESCRIPTION:

When no low bound is specified for a dimension, the default is 1. The OPTION BASE statement lets you specify a default low bound of 0 or 1. When any following DIM or REDIM statements are executed, SheerPower defaults the low bound to 0 or 1 as specified.


Previous Next Contents Index