User's Guide - Building Expressions and Statements

## 10.4 Building Expressions and Statements

You can use the basic elements of the Data Explorer scripting language to build expressions and assignment statements. Most statements in the scripting language are assignment statements; however, a special group of script commands with their options can form a statement. These commands are described in 10.7 , "Using Data Explorer Script Commands". The following sections tell you how to write Data Explorer script expressions and statements.

### Arithmetic Expressions

You can combine scalar values and variables that contain values with the arithmetic operators listed below to derive new values. Lists of scalars can also be combined if the lists have the same cardinality (number of elements), or if one of the lists has just a single element. If the two lists being operated upon have the same number of elements, then the operator is applied to the corresponding element pairs in each list to produce a new list of the same cardinality. If one of the lists has just a single element, then the operator is applied, in turn, to each of the elements of the larger list and to that single element to produce a list whose size is the same as the larger list.

If both of the elements in an operation are the same type, either integer or floating point, then the result of the operation is also of the same type. If, however, one of the elements is an integer and the other is a floating-point value, then the result is a floating-point value. Given this automatic type conversion, a simple way to convert an integer value to a floating-point value is to add 0.0 to it.

#### Operators

The operators listed in the following table can be used in arithmetic expressions. In the table, a horizontal line separates each precedence level and the higher levels are placed above the lower ones. Operators with higher precedence are evaluated before those with lower precedence. Operators with the same precedence are evaluated from left to right. Because expressions in parentheses are evaluated first, you can use parentheses to alter the grouping of operands, thereby changing the precedence levels.

Operator | Description
--- | ---
`-` | Unary minus
`^` | Exponentiation
`**` | Exponentiation (alternate form)
`*` | Multiplication
`/` | Division
`+` | Addition
`-` | Subtraction

The following expressions all have the value {2.0 4.0 8.0}:

    {3.0 5.0 9.0} - {1.0 1.0 1.0}
    {2 4 8} + 0.0
    8 * {.25 .50 1e0}
    2 ^ {1.0 2.0 3.0}
    ({2 2 2} - 1.0) * {2 4 8}

### Assignment Statements

Assignment statements store values in variables. The general form of an assignment statement is:

    left-side[attribute_name:value,...] =
    right-side[attribute_name:value,...]

The left-side portion of an assignment statement is a sequence of one or more identifiers separated by commas. The `[attribute_name:value,...]` value pair lists and the brackets are optional, and are discussed in "Function Call Attributes". The equal sign (`=`) is the assignment operator. It stores the values specified in the right-side portion of the statement in the variables named by the identifiers specified in the left-side portion of the statement. You can specify the values in the right-side portion of the statement as a sequence of expressions or as the result of a single function call.

You can also use either of two additional symbols, `<-` or `:=`, as the assignment operator. An assignment statement is terminated with a semicolon, indicating the end of the right-side portion of the statement.

The values specified to the right of the assignment operator are assigned to the identifiers to the left of the assignment operator. If the number of values equals the number of identifiers, the first value is stored in the first identifier, the second in the second, the third in the third, and so on. If the number of values is greater than the number of identifiers, the extra rightmost values are ignored. If the number of values is less than the number of identifiers, the values are assigned in order to the leftmost identifiers.

Those identifiers not receiving a value from the right-side list are set to the value `NULL`. In addition, if an identifier is repeated in the left-side list, then the right-side associated with that identifier is the value associated with its rightmost instance.

All identifiers that have not had a value explicitly assigned to them have the value `NULL`.

#### Expression Assignments

The values for the right-side portion of an expression assignment statement consist of a sequence of constant values, variables, arithmetic expressions, and the value `NULL`, separated by commas. The various values need not be of the same kind, data type, or dimension.

The following examples all assign the value "A string" to the variable a, the value 2.0 to the variable b, and the value `NULL` to the variable c.

    a  = "A string";                // These 3 lines
    b := 2 * (2 - 1e0);             // constitute a single
    c <- NULL;                      // example.

    a, b, c     = "A string", 2.0, NULL;
    a, b, c    <- "A string", 5 / 2.5, NULL;
    a, b, c     = "A string", 2.0;
    a, b, c, d  = "A string", 2.0;
    a, b, c, a  = [[1 0][0 1]], 2.0, NULL, "A  string",  [1 2 3];

The following example illustrates a simple way to swap values:

    a = 2;
    b = 4;
    c = 6;
    d = 8;
    e = 10;
    a, b = b, a;         // Values are swapped, so a = 4, b = 2
    c, d, e = e, c, d;   // c = 10, d = 6, e = 8

### Function Call Assignments

A function call can refer either to a function defined as a module (a function compiled into the system), or to a macro (a function defined in the scripting language itself). The values for the right-side portion of a function call assignment statement are the values returned by a single function call.

The Statistics function, which is used in the following examples, returns five values:

- Mean of the data
- Standard deviation of the data
- Variance of the data
- The minimum value in the data
- The maximum value in the data

In the first example, all of these values are assigned to variables for later use. In the second example, the minimum and maximum values are ignored. In the third example, only the minimum and maximum values are being saved for later use.

     (1)  mean, sd, var, min, max = Statistics (data);
     (2)  mean, sd, var = Statistics (data);
     (3)  min, min, min, min, max = Statistics (data);

[ OpenDX Home at IBM | OpenDX.org ]