Declaring arrays
Description
An array is an indexed collection of elements of a single
datatype. In PowerBuilder, an array can have one or more dimensions.
One-dimensional arrays can have a fixed or variable size; multidimensional
arrays always have a fixed size. Each dimension of an array can
have 2,147,483,647 bytes of elements.
Any simple variable declaration becomes an array when you
specify brackets after the variable name. For fixed-size arrays,
you specify the sizes of the dimensions inside those brackets.
Syntax
1 |
{ <span>access</span> } <span>datatype</span> <span>variablename</span> { <span>d1</span>, ..., <span>dn</span> } { = { <span>valuelist </span>} } |
The following table describes the parameters used to declare
array variables.
Parameter |
Description |
||
---|---|---|---|
access |
(For instance variables only) Keywords |
||
datatype |
The datatype of the variable. You can For decimals, you can specify the precision of the data by including
For blobs, fixed-length blobs within an array are not supported. If |
||
variablename |
The name of the variable (name must be You can define additional arrays with the same datatype by naming |
||
[ { d1, |
Brackets and (for fixed-size arrays) For a variable-size array, which is always one-dimensional, specify For more information on how variable-size arrays For a fixed-size array, the number of dimensions is determined by For fixed-size arrays, you can use TO to
|
||
{ valuelist } |
A list of initial values for each position |
Examples
These declarations create variable-size arrays:
1 |
integer li_stats[ ]     // Array of integers. |
1 |
decimal {2} ld_prices[ ]  // Array of decimals with |
1 |
// 2 places of precision. |
1 |
blob lb_data[ ]       // Array of variable-size |
1 |
// blobs. |
1 |
date ld_birthdays[ ]    // Array of dates. |
1 |
string ls_city[ ]      // Array of strings. |
1 |
// Each string can be |
1 |
// any length. |
This statement declares a variable-size array of decimal number
(the declaration does not specify a precision, so each element in
the array takes the precision of the value assigned to it):
1 |
dec lc_limit[ ] |
Fixed arrays
These declarations create fixed-size, one-dimensional arrays:
1 |
integer li_TaxCode[3]  // Array of 3 integers. |
1 |
string ls_day[7]       // Array of 7 strings. |
1 |
blob ib_image[10]      // Array of 10 |
1 |
                       // variable-size blobs. |
1 |
dec{2} lc_Cost[10]     // Array of 10 decimal |
1 |
                       // numbers. |
1 |
                       // Each value has 2 digits |
1 |
                       // following the decimal |
1 |
                       // point. |
1 |
decimal lc_price[20]   // Array of 20 decimal |
1 |
                       // numbers. |
1 |
                       // Each takes the precision |
1 |
                       // of the value assigned. |
Using TO to change array index values
These fixed-size arrays use TO to change
the range of index values for the array:
1 |
real lr_Rate[2 to 5]      // Array of 4 real numbers: |
1 |
                           // Rate[2] through Rate[5] |
1 |
integer li_Qty[0 to 2]     // Array of 3 integers |
1 |
string ls_Test[-2 to 2]    // Array of 5 strings |
1 |
integer li_year[76 to 96]  // Array of 21 integers |
1 |
string ls_name[-10 to 15]  // Array of 26 strings |
Incorrect declarations using TO
In an array dimension, the second number must be greater than
the first. These declarations are invalid:
1 |
integer li_count[10 to 5]     // INVALID: 10 is |
1 |
                              // greater than 5 |
1 |
integer li_price[-10 to -20]  // INVALID: -10 |
1 |
                              // is greater than -20 |
Arrays with two or more dimensions
This declaration creates a six–element, two-dimensional
integer array. The individual elements are li_score[1,1], li_score[1,2], li_score[1,3], li_score[2,1], li_score[2,2],
and li_score[2,3]:
1 |
integer li_score[2,3] |
This declaration specifies that the indexes for the dimensions
are 1 to 5 and 10 to 25:
1 |
integer li_RunRate[1 to 5, 10 to 25] |
This declaration creates a 3-dimensional 45,000-element array:
1 |
long ll_days[3, 300, 50] |
This declaration changes the subscript range for the second
and third dimension:
1 |
integer li_staff[100, 0 to 20, -5 to 5] |
More declarations of multidimensional arrays:
1 |
string ls_plant[3,10]   // two-dimensional array |
1 |
                        // of 30 strings |
1 |
dec{2} lc_rate[3,4]     // two-dimensional array of 12 |
1 |
                        // decimals with 2 digits |
1 |
                        // after the decimal point |
This declaration creates three decimal arrays:
1 |
decimal{3} lc_first[10],lc_second[15,5],lc_third[ ] |