ARRAY-ADD, ARRAY-DIVIDE, ARRAY-MULTIPLY, ARRAY-SUBTRACT
Syntax
ARRAY-ADD{src_num_lit|_var|_col}...TO dst_array_name (element_lit|_var|_col)[field [(occurs_lit|_var|_col)]]... ARRAY-DIVIDE{src_num_lit|_var|_col}...INTO dst_array_name (element_int_lit|_var|_col)[field [(occurs_lit|_var|_col)]]... ARRAY-MULTIPLY{src_num_lit|_var|_col}...TIMES dst_array_name (element_int_lit|_var|_col)[field [(occurs_lit|_var|_col)]]... ARRAY-SUBTRACT{src_num_lit|_var|_col}...FROM dst_array_name (element_int_lit|_var|_col)[field [(occurs_lit|_var|_col)]]...
Description
These four commands perform arithmetic operations on one or more elements in an array.
The following information applies to the array arithmetic commands:
-
The array must first be created with the CREATE-ARRAY command.
-
The four array arithmetic commands perform on one or more source numbers, placing the result into the corresponding field in the array.
-
Array element and field occurrence numbers can be numeric literals (123) or numeric variables (#j) and can be from zero (0) to one less than the size of the array.
-
If fields are not listed, the results are placed into consecutively defined fields in the array. If fields are listed, results are placed into those fields at the specified occurrence of the field. If an occurrence is not specified, the zeroth (0) occurrence is used.
-
All fields must be of type number, decimal, float, or integer. They cannot be of type date, char, or text.
-
If division by zero is attempted, a warning message appears, the result field is unchanged, and SQR continues running.
Parameters
| Parameter | Description |
|---|---|
|
src_num_lit|_var|_col |
Source values are added to, divided into, multiplied by, or subtracted from the respective destination array fields. All variables must be numeric in type. |
|
dst_array_name ( element_int_lit|_var|_col) [ field [ ( occurs_lit|_var|_col) ] ] |
Destination array fields contain the results after the operation. All variables must be numeric in type. |
Example
The following example adds &salary and #comm to the first two fields defined in the emps array. The #j'th element of the array is used:
array-add &salary #comm to emps(#j)
The following example subtracts #lost, #count, and 1 from the fields loses, total, and sequence of the #j2'th element of the stats array:
array-subtract #lost #count 1 from stats(#j2) loses total sequence
The following example multiplies occurrences 0 through 2 of the field p in the #i'th element of the percentages array by 2:
array-multiply 2 2 2 times percentages(#i) p(0) p(1) p(2)
The following example divides the #i2'th occurrence of the salesman field of the #j'th element of the commissions array by 100:
array-divide 100 into commissions(#j) salesman(#i2)
The following example uses the ARRAY-ADD command in an SQR program:
begin-setup
! declare arrays
create-array name=emps size=1 ! one row needed for this example
field=Salary:number=35000 ! initialize to 35,000
field=Comm:number=5000 ! initialize to 5,000
end-setup
begin-program
do Main
end-program
begin-procedure Main local
! Show original contents of the arrays, then the modified arrays
! array-add
! retrieve values from the only row of array "emps"
get #sal #com FROM emps(0) Salary Comm
print 'Array-Add' (+1, 1)
print 'Add 1000 to each column' (+1, 1)
print 'Salary' (+1, 3) bold underline
print 'Comm' (,25) bold underline
print #sal (+1, 1) money
print #com (,22) money
let #salary = 1000
let #commission = 1000
let #j = 0 ! address the array row with variable "#j"
! Add 1000 (in variables) to each column of row 0 (the 1st and only row)
array-add #salary #commission TO emps(#j)
! retrieved the new "added" values
get #sal #com FROM emps(0) Salary Comm
print #sal (+1,1) money
print #com (,22) money
end-procedure
See The CREATE-ARRAY command
for information about creating an array.
See The CLEAR-ARRAY command
for information about clearing or initializing an array.
See The GET, PUT, and LET commands
for information about using arrays.