The Calculation component allows you to compose complex calculations or simply assign data to a field or variable. Since the Calculation component is adjustable in size, calculations are not hampered by any space considerations. In addition multiple Calculation components can be positioned in succession in the Workspace. A Calculation component can also contain more than one statement in which case it executes them in a top-down order:
In the process of performing various operations on variables, the Calculation component uses statements which can be as simple as "target = expression;" where:
The following are simple examples of valid statements:
| 1. | Amount = Price * Quantity; | Multiply the contents of the Price variable by the contents of the Quantity variable; store the resulting value in the Amount variable. |
| 2. | Quantity = Quantity - 1; | Subtract 1 from the contents of the Quantity variable; store the resulting value back into the Quantity variable. |
| 3. | Date = '19940115'; | Store date in the internal format of YYYYMMDD in the Date variable. |
| 4. | Name = 'John Doe'; | Store the character string "John Doe" in the Name variable. |
| 5. | CityString = 'Calgary, ' + 'AB, ' + 'Canada'; | Concatenates (strings together) 'Calgary, ' 'AB, ' 'Canada' and stores the resulting text string "Calgary, AB, Canada" in the CityString variable. |
| 6. | EffectivePrice = Price * (1.0 - PercentDiscount * 0.01); | Calculate the effective price as the product of the original price carried by the Price variable and discount factor; the discount factor is calculated as the difference between 1 and the percentage of the discount stored in the PercentDiscount variable (note the priority of the operations: the expression in the parentheses is calculated first). |
| 7. | Flag = Amount > CreditLimit; | Compare contents of the Amount variable with contents of the CreditLimit variable; if the former is > than the latter, assign logical value YES to the Flag variable; else, set it to NO. |
Scribe recognizes the following data types in Calculation statements:
| Type | Description | |
| TEXT | Free-formatted ASCII text | |
| DATE | Date in YYYYMMDD format | |
| TIME | Time in HHMMSS format | |
| INTEGER | A whole number without decimal point | |
| FLOAT | A regular number with decimal point | |
| MONEY | Money (similar to FLOAT) | |
| LOGICAL | Logical value that can be set to YES or NO | |
| SEARCHKEY | Search key | |
| FILENAME | Name of file |
The data type of the target variables in a Calculation statement is not explicitly assigned by the user, but is instead inferred from the context of the procedure. It is derived from the data type of the right-hand side of the equation, which in turn depends on the data types of variables and constants used in the expression.
In the simplest case, where the statement is of type target = constant, the target inherits data type of the constant. For example, Quantity = 1 sets the data type of Quantity to INTEGER, because this is the data type of constant 1. In another example, Industry = 'Automotive' makes the Industry variable of TEXT type, because the value assigned to it is enclosed in single quotes (and therefore, by definition, is a literal constant).
In the case of a more complex expression, where the right-hand side of the statement is composed of variables and constants of (possibly) different data types, evaluation of the resulting data type of the whole expression is done incrementally, in the following steps:
| 1. | The original expression is parsed into a series of elementary sub-expressions of A <op> B type, where A and B are variables or constants, and <op> is arithmetic, comparison or logical operator. This breaking-up of the expression is done according to the grouping of components of the expression (with parentheses), in the order of priorities. The example that follows illustrates this process:
|
|||||||
| 2. | Data type of each of the sub-expressions is evaluated according to the following rules:
|