EVAL (Evaluate expression)

Free-Form Syntax {EVAL{(HMR)}} result = expression
{EVAL{(HMR)}} result += expression
{EVAL{(HMR)}} result -= expression
{EVAL{(HMR)}} result *= expression
{EVAL{(HMR)}} result /= expression
{EVAL{(HMR)}} result **= expression
Code Factor 1 Extended Factor 2
EVAL (H M/R)   Assignment Statement

The EVAL operation code evaluates an assignment statement of the form "result = expression" or "result op = expression". The expression is evaluated and the result placed in result. Therefore, result cannot be a literal or constant but must be a field name, array name, array element, data structure, data structure subfield, or a string using the %SUBST built-in function.

The expression may yield any of the RPG data types. The type of the expression must be the same as the type of the result. A character, graphic, or UCS-2 result will be left justified and padded with blanks on the right or truncated as required. If result is a variable-length field, its length will be set to the length of the result of the expression.

If the result represents an unindexed array or an array specified as array(*), the value of the expression is assigned to each element of the result, according to the rules described in Specifying an Array in Calculations. Otherwise, the expression is evaluated once and the value is placed into each element of the array or sub-array. For numeric expressions, the half-adjust operation code extender is allowed. The rules for half adjusting are equivalent to those for the arithmetic operations.

On a free-form calculation specification, the operation code name may be omitted if no extenders are needed, and if the variable does not have the same name as an operation code.

For the assignment operators +=, -=, *=, /=, and **=, the appropriate operation is applied to the result and the expression, and the result is assigned to the result. For example, statement X+=Y is roughly equivalent to X=X+Y. The difference between the two statements is that for these assignment operators, the result operand is evaluated only once. This difference is significant when the evaluation of the result operation involves a call to a subprocedure which has side-effects, for example:
     warnings(getNextCustId(OVERDRAWN)) += 1;

See Expressions for general information on expressions. See Precision Rules for Numeric Operations for information on precision rules for numeric expressions. This is especially important if the expression contains any divide operations, or if the EVAL uses any of the operation extenders.

Figure 1. EVAL Operations
 *..1....+....2....+....3....+....4....+....5....+....6....+....7...+....
 *                   Assume FIELD1 = 10
 *                          FIELD2 =  9
 *                          FIELD3 =  8
 *                          FIELD4 =  7
 *                          ARR is defined with DIM(10)
 *                          *IN01 = *ON
 *                          A = 'abcdefghijklmno' (define as 15 long)
 *                          CHARFIELD1 = 'There'  (define as  5 long)

 /FREE
   // The content of RESULT after the operation is 20
   eval RESULT=FIELD1 + FIELD2+(FIELD3-FIELD4);
   // The indicator *IN03  will be set to *ON
   *IN03 = *IN01 OR (FIELD2 > FIELD3);
   //  Each element of array ARR will be assigned the value 72
   ARR(*) = FIELD2 * FIELD3;
   //  After the operation, the content of A = 'Hello There   '
   A = 'Hello ' + CHARFIELD1;
   //  After the operation the content of A = 'HelloThere    '
   A = %TRIMR('Hello ') + %TRIML(CHARFIELD1);
   //  Date in assignment
   ISODATE = DMYDATE;
   //  Relational expression
   //  After the operation the value of *IN03 = *ON
   *IN03 = FIELD3 < FIELD2;
   //  Date in Relational expression
   // After the operation, *IN05 will be set to *ON if Date1 represents
   // a date that is later that the date in Date2
   *IN05 = Date1 > Date2;
   // After the EVAL the original value of A contains 'ab****ghijklmno'
   %SUBST(A(3:4))= '****';
   // After the EVAL PTR has the address of variable CHARFIELD1
   PTR = %ADDR(CHARFIELD1);
   //  An example to show that the result of a logical expression is
   //  compatible with the character data type.
   //  The following EVAL statement consisting of 3 logical expressions
   //  whose results are concatenated using the '+' operator
   //  The resulting value of the character field RES is '010'
   RES = (FIELD1<10) + *in01 + (field2 >= 17);
   // An example of calling a user-defined function using EVAL.
   // The procedure FormatDate converts a date field into a character
   // string, and returns that string.  In this EVAL statement, the
   // field DateStrng1 is assigned the output of formatdate.
   DateStrng1 = FormatDate(Date1);
   // Subtract value in complex data structure.
   cust(custno).account(accnum).balance -= purchase_amount;
   // Append characters to varying length character variable
   line += '<br />';
 /END-FREE