# a (algebraic) processing code

The a processing code is a recursive
algebraic function that creates algebraic formulas and relational
operations consisting of operands and operators; these expressions
are used by AQL as well as the B-tree indexes. The a processing codes are translated into f processing
codes by the AQL compiler. The functional operators and operands for
the a processing code are the same as that of the f processing code, except as noted.

| System Processors | Select, Update, List |
| --- | --- |
| Code Type | Attribute Defining |
| Dictionary Attributes | Input Conversion, Output Conversion, Correlative |

## Syntax

```
a{{num.digits;}expressions...}
```

## Parameter(s)

| num.digits | Argument optionally used to indicate the number of decimal digits (in the range 0 through 4) to retain during calculations involving a mixture of whole numbers and numbers with implied decimals, along with m (mask) processing codes in the body of the function. The default is 0. |
| --- | --- |
| expression{s} | Expressions formed are like FlashBASIC expressions and consist of operands, operators, conditional statements, and special functions combined together to yield a single resulting value. |
D3 algebraic processing uses a *last-in first-out* stack. Operands are pushed onto the top of the stack and all existing
stack entries shift down. Operations operate on the top first, second,
or third stack entries depending on the operation. Results processing,
including additional conversions, take the top value off the stack.

## Operands

| number | An integer number reference to an attribute count. For example: ```
a3*5
``` This multiplies the value in attribute 3 by the value in attribute 5. |
| --- | --- |
| number | A reference to an attribute count, with a repeat code, meaning that the first value of the attribute is to be used (repeated) when using this attribute against another MultiValue attribute. For example: ```
a3r*5
``` This multiplies the first value in attribute 3 by the first value in attribute 5, and returns the result as the first value. Then, it multiplies the first value in attribute 3 by the second value in attribute 5, and returns it as the second value, and so on. |
| number(pr.code{]pr.code...}) | An integer number reference to an attribute count, that may be immediately passed through any other valid processing code, (except another a or f processing code or an index processing code). Multiple processing codes may be specified and each separate processing code must be delimited by a value mark (represented in the syntax with the ] character). For example: ```
a1(t1,40]mct)
``` This retrieves attribute 1 of the current item, extracts the first 40 characters, and converts each word to upper and lowercase. Note: Time and date subconversion codes are also supported for FSI indexes and FlashBASIC running as a rule module. |
| 9998 | Used as an attribute count specification. Returns the sequential item counter. |
| 9999 | Used as an attribute count specification. Returns the size of the item in bytes. |
| "text" | Any quoted text or numeric string, which is treated as a literal. For example: ```
a"Attention: ":1
``` This concatenates the literal, "Attention: ", to the contents of attribute 1 of the current item. An example of a numeric constant is: ```
a3*"12"
``` This multiplies the value of attribute 3 of the current item by the constant "12". |
| n(attrname) {(pr.code...)} | In this case, n is a literal and indicates that the ADI within the following set of parentheses is to be retrieved. This is sometimes called an indirect reference. This allows a reference to any valid ADI in the dictionary of the file being accessed. The string returned with this operand may subsequently be passed through one or more other valid processing codes (except another a or f processing code or an index processing code) as long as each is separated by a value mark. This is where recursion occurs, as the attribute name being referenced in the processing code may in fact reference an item that also contains an a processing code. For example: ```
an(city):", ":n(state):" ":n(zip)
``` This concatenates the contents of the attribute defined as city with a comma and a space, then joins the value of the attribute defined as state with a space and then appends the attribute defined as zip to the end of the string. For example: ```
an(city):", ":n(state)(tstates;c;;1):" ":n(zip)
``` This is like the previous example, but this time the value in the state attribute is translated to the states file, returning attribute 1 of the corresponding item. |
| d | Returns the system date in internal form. |
| lpv | Last processing code value (result of the previous processing code). |
| nb | Returns the number of the break level. There is a maximum of 255 break levels (0 through 254). The 255th level is reserved for the grand total. This works in attribute 7, the output conversion attribute only. |
| nd | Returns the number of detail lines. This works in attribute 7, the output conversion attribute only. |
| ni | Returns the sequential number of the item being processed. This works in attribute 7, the output conversion attribute only. |
| ns | Returns the sequential number of the subvalue being processed. |
| nv | Returns the sequential number of the value being processed. For example: ```
nv=s(ac#’!’)
``` |
| t | Returns the system time in internal format, meaning the number of seconds past midnight. |

## Operators

| Arithmetic Operators | + | Adds |
| --- | --- | --- |
| * | Multiplies | |
| - | Subtracts | |
| / | Divides (note that division operations are rounded down) | |
| Concatenation operator | : | Concatenates two operands. |
| Boolean Operators | AND | Logically AND operand one to operand two. |
| OR | Logically OR operand one to operand two. | |
| Arithmetic Functions | ```
r(expression1, expression2)
``` Returns the remainder of expression1 divided by expression2. ```
s(expression)
``` Sums MultiValue results. | |
| Substring References | ```
string.expression[begexp, lenexp]
``` Extracts a fixed number of characters from a string expression. The begexp (beginning position expression) specifies the beginning character position and the lenexp (length expression) specifies the length, or number, of characters to retrieve. The beginning expression and length expression may be quoted numbers or any expressions that derive numeric results. For example: ```
a"(":n(phone)["1","3"]:")":n(phone)["4", "3"]:"-":n(phone)["7","4"]
``` This takes a phone number stored as 7145551212 and outputs it as (714) 555-1212. | |
| Relational Operators | Produce a numeric result. These results are either 1 for a true condition or 0 for a false condition. | |
| = | Equal to. | |
| # | Not equal. | |
| = | Greater than or equal to. | |
| Conditionals | Are constructed using a syntax very similar to the FlashBASIC if...then...else construct and have the general form: if condition then algebraic function {else algebraic function} {end} if condition else algebraic function {end} if condition then algebraic function {end} If the condition evaluates to 0, it is considered false and (if present) the else clause is taken. If the evaluation is false, and the else clause is missing, a null is returned. If the condition evaluates to nonzero, it is considered true, and (if present) the then clause is taken. If the evaluation is true, and there is no then clause, a null is returned. If end is not specified, the else is paired with the previous if. The end is used to close off the current if and associate the else with the previous if. | |
| Conditional options | condition | An algebraic function (a processing code). |
| algebraic function | An algebraic function (a processing code). Examples: ```
if 1 > 2 then n(attr1) else n(attr2) end
if 1 > 2 then if 3 then 3 else 4
if (if 1 then 2 else 3) then "true"
``` | |
Warning: Arithmetic within a processing code
is always integer arithmetic, so any value with a decimal fraction
is truncated.

 The attribute values should be stored without
a decimal point, and are reconstituted upon output, that is:

- Value entered is 100.25

- Input processing code mr2 stores the value as 10025

- Output processing code mr2 recreates the original value, and outputs as 100.25

 Performing arithmetic on two internal values is done as two
integers, and care should be taken to rescale the result properly
to preserve the proper placement of the decimal place.

 Division
by 0 produces a result of 0 with no error message.

 If arithmetic
must be performed on internal values that contain a decimal point,
then the processing code must *normalize* the value to a number
with a fixed number of decimal place, and descale the result before
output. For example:

```
a(((x(g0.1)*"100000")+(x(g1.1)(ml%5)))*((y(g0.1)*"100000")+(y(g1.1)(ml%5)))/"100000")(mr5)
```
where:

| x | Uses attribute number. |
| --- | --- |
| x(G0.1) | Retrieves the integer part. |
| x(G1.1) | Retrieves the decimal part. |
| *”100000” | Scales the integer portion. |
| (S;*;"0") | Forces trailing zeroes if there is not any decimal point. |
| /”100000” | Scales the integer result. |
| (ML%5) | Normalizes the decimal part into five digits. |
| (MR5) | Outputs the number with five decimal places. |
Since this must be done with each attribute, the structure of the data can avoid this situation,
 meaning that it is a good idea to try to ensure that the raw data in decimal attributes not
 contain decimal points.

 Also, it has been reported
that the a processing codes do not handle index
processing codes. For example, using an input-conversion of `A1(Ifilename;A0)` results in the error message:

```
[367] illegal f-correlative.: ’A1(Ifilename;A0’
```

Note: **A-correlatives are translated to F-correlatives at AQL runtime. If you have an ADI with an A-correlative in which you would rather use an F-correlative, use the (Y AQL option to show the translation of the A-correlative to an F-correlative. Example: LIST ORDERS ORD.DATE (Y**

## Example(s)

**Example 1**

Outputs a
 literal “Hello”:

```
 a;"Hello"
```
**Example 2**

Outputs
 the value of attribute 9 subtracted from attribute
 18:

```
 a;18-9
```
**Example 3**

Sums the products of
 attribute 26 times attribute 30 and attribute 26 times attribute 29, then subtracts that from
 the product of attribute 26 times attribute
 27:

```
 a;(26*27)-((26*30)+(26*29))
```
**Example 4**

Creates a 6-digit year-month period by doing a DY output conversion on attribute 9
 and concatenating a DM output conversion on attribute 9, the month portion being right justified
 into a 2-byte zero-filled field before the
 concatenation:

```
 a9(dy):9(dm)(mr%2)
```
**Example 5**

Uses attribute 8 as an item ID to read attribute 2 of a file, then uses the result
 as an item ID to read attribute 1 of another
 file:

```
 a8(tterritories;x;;2)(tsalesreps;x;;1)
```
****

## See also

- [*a9999](https://d3codex.com/attributedefiningitem/asterisk-a9999/)
- [B-tree](https://d3codex.com/definitions/b-tree/)
- [c (Concatenate) processing code](https://d3codex.com/processingcodes/c-concatenate-processing-code/)
- [Processing codes (Update processor)](https://d3codex.com/updateprocessor/processing-codes-update-processor/)
- [Processing codes (Update processor)](https://d3codex.com/updateprocessor/processing-codes-update-processor/)
- [create-index command](https://d3codex.com/tcl/create-index-command/)
- [delete-index command](https://d3codex.com/tcl/delete-index-command/)
- [f (f-correlative) processing code](https://d3codex.com/processingcodes/f-f-correlative-processing-code/)
- [i (local index) processing code](https://d3codex.com/processingcodes/i-local-index-processing-code/)
- [id (item ID) processing code](https://d3codex.com/processingcodes/id-item-id-processing-code/)
- [if (if/then) processing code](https://d3codex.com/processingcodes/if-if-then-processing-code/)
- [Processing codes (Update processor)](https://d3codex.com/updateprocessor/processing-codes-update-processor/)
- [nframe-index command](https://d3codex.com/tcl/nframe-index-command/)
- [output-conversion](https://d3codex.com/attributedefiningitem/output-conversion/)
- [Processing codes (Update processor)](https://d3codex.com/updateprocessor/processing-codes-update-processor/)
- [Processing codes overview](https://d3codex.com/processingcodes/processing-codes-overview/)
- [Processing codes (Update processor)](https://d3codex.com/updateprocessor/processing-codes-update-processor/)
- [f (f-correlative) processing code](https://d3codex.com/processingcodes/f-f-correlative-processing-code/)
- [root statement](https://d3codex.com/pickbasic-flashbasic/root-statement/)
- [s (substitution) processing code](https://d3codex.com/processingcodes/s-substitution-processing-code/)
- [u01ad user exit](https://d3codex.com/proc/u01ad-user-exit/)
- [verify-index command](https://d3codex.com/tcl/verify-index-command/)

---
Source: https://d3codex.com/processingcodes/a-algebraic-processing-code/ - part of the D3Codex reference.
