Assigning one array to another
General information
When you assign one array to another, PowerBuilder uses the
following rules to map the values of one onto the other.
One-dimensional arrays
To an unbounded array
The target array is the same as the source:
|
1 |
integer a[ ], b[ ] |
|
1 |
a = {1,2,3,4} |
|
1 |
b = a |
To a bounded array
If the source array is smaller, values from the source array
are copied to the target array and extra values are set to zero.
In this example, b[5] and b[6] are
set to 0:
|
1 |
integer a[ ], b[6] |
|
1 |
a = {1,2,3,4} |
|
1 |
b = a |
If the source array is larger, values from the source array
are copied to the target array until it is full (and extra values
from the source array are ignored). In this example, the array b has
only the first three elements of a:
|
1 |
integer a[ ], b[3] |
|
1 |
a = {1,2,3,4} |
|
1 |
b = a |
Multidimensional arrays
PowerBuilder stores multidimensional arrays in column major
order, meaning the first subscript is the fastest varying—[1,1], [2,1], [3,1]).
When you assign one array to another, PowerBuilder linearizes
the source array in column major order, making it a one-dimensional
array. PowerBuilder then uses the rules for one-dimensional arrays
(described above) to assign the array to the target.
Not all array assignments are allowed, as described in the
following rules.
One multidimensional array to another
If the dimensions of the two arrays match, the target array
becomes an exact copy of the source:
|
1 |
integer a[2,10], b[2,10] |
|
1 |
a = b |
If both source and target are multidimensional but do not
have matching dimensions, the assignment is not allowed and the
compiler reports an error:
|
1 |
integer a[2,10], b[4,10] |
|
1 |
a = b // Compiler error |
One-dimensional array to a multidimensional array
A one–dimensional array can be assigned to a multidimensional
array. The values are mapped onto the multidimensional array in
column major order:
|
1 |
integer a[ ], b[2,2] |
|
1 |
b = a |
Multidimensional array to a one-dimensional array
A multidimensional array can also be assigned to a one-dimensional
array. The source is linearized in column major order and assigned
to the target:
|
1 |
integer a[ ], b[2,2] |
|
1 |
a = b |
Examples
Suppose you declare three arrays (a, b,
and c). One (c) is unbounded
and one–dimensional; the other two (a and b)
are multidimensional with different dimensions:
|
1 |
integer c[ ], a[2,2], b[3,3] = {1,2,3,4,5,6,7,8,9} |
Array b is laid out like this:
|
|
|
||||||
|
|
|
||||||
|
|
|
This statement causes a compiler error, because a and b have
different dimensions:
|
1 |
a = b // Compiler error |
This statement explicitly linearizes b into c:
|
1 |
c = b |
You can then assign the linearized version of the array to a:
|
1 |
a = c |
The values in array a are laid out like
this:
|
|
||||
|
|
Initializing a with an arraylist produces
the same result:
|
1 |
integer a[2,2] = {1,2,3,4} |
The following section describes arraylists.