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 |
{ access } datatype variablename { d1, ..., dn } { = { valuelist } } |
The following table describes the parameters used to declare array
variables.
|
Parameter |
Description |
||
|---|---|---|---|
|
access (optional) |
(For instance variables only) Keywords specifying the |
||
|
datatype |
The datatype of the variable. You can specify a For decimals, you can specify the precision
For |
||
|
variablename |
The name of the variable (name must be a valid You can define additional arrays with |
||
|
[ { d1, …, dn } ] |
Brackets and (for fixed-size arrays) one or more For a variable-size For more information on how variable-size arrays For a fixed-size For fixed-size arrays, you can use
|
||
|
{ valuelist } (optional) |
A list of initial values for each position of the |
Examples
These declarations create variable-size arrays:
|
1 2 3 4 5 6 7 8 9 |
integer li_stats[ ] // Array of integers. decimal {2} ld_prices[ ] // Array of decimals with // 2 places of precision. blob lb_data[ ] // Array of variable-size // blobs. date ld_birthdays[ ] // Array of dates. string ls_city[ ] // Array of strings. // Each string can be // 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 2 3 4 5 6 7 8 9 10 11 12 13 |
integer li_TaxCode[3] // Array of 3 integers. string ls_day[7] // Array of 7 strings. blob ib_image[10] // Array of 10 // variable-size blobs. dec{2} lc_Cost[10] // Array of 10 decimal // numbers. // Each value has 2 digits // following the decimal // point. decimal lc_price[20] // Array of 20 decimal // numbers. // Each takes the precision // 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 2 3 4 5 6 |
real lr_Rate[2 to 5] // Array of 4 real numbers: // Rate[2] through Rate[5] integer li_Qty[0 to 2] // Array of 3 integers string ls_Test[-2 to 2] // Array of 5 strings integer li_year[76 to 96] // Array of 21 integers 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 2 3 4 |
integer li_count[10 to 5] // INVALID: 10 is // greater than 5 integer li_price[-10 to -20] // INVALID: -10 // 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 2 3 4 5 |
string ls_plant[3,10] // two-dimensional array // of 30 strings dec{2} lc_rate[3,4] // two-dimensional array of 12 // decimals with 2 digits // after the decimal point |
This declaration creates three decimal arrays:
|
1 |
decimal{3} lc_first[10],lc_second[15,5],lc_third[ ] |