EVAL (式の評価)

コード 演算項目 1 拡張演算項目 2
EVAL (H M/R) (EVAL (H M/R))   割り当てステートメント

EVAL 命令コードは、 "result = expression" または "result op = expression"の形式の割り当てステートメントを評価します。 式が評価されると結果が結果に入れられます。 したがって、結果をリテラルまたは定数とすることはできず、 フィールド名、配列名、配列要素、データ構造、データ構造サブフィールド、 または %SUBST 組み込み関数を使用したストリングとしなければなりません。

式では任意の RPG データ・タイプを生成することができます。 式のタイプは結果のタイプと同じでなければなりません。 文字、図形、または UCS-2 の結果は左寄せされ、必要に応じて右側にブランクが埋め込まれるか切り捨てられます。 結果が可変長フィールドの場 合、その長さは式の結果の長さに設定されます。

CHARCOUNT モードが EVAL に与える影響については、 異なる文字サイズのデータの割り当てを参照してください。

結果が索引なし配列または配列 (*) として指定された配列を表す場合、式の値は、「 計算での配列の指定」で説明されている規則に従って、結果の各エレメントに割り当てられます。 そうでない場合には、式が一度評価されて、その値が配列またはサブ配列のそ れぞれの要素に入れられます。 数値式の場合には、四捨五入の命令コード拡張を使用することができます。 四捨五入の規則は、算術演算の場合の規則と同じです。

自由形式演算仕様においては、拡張が不要である場合、および変数に命令コードと同じ名前がない場合は、命令コード名を省略できます。

割り当て演算子 +=、-=、*=、/=、および **= の場合、該当する演算は結果および式に適用され、 結果は結果に割り当てられます。 例えば、ステートメント X+=Y は X=X+Y とほぼ等しくなります。 この 2 つのステートメントの違いは、これらの割り当て演算子の場合、結果オペランドが評価されるのは 1 回のみであるということです。 これらの違いは、 結果命令を評価するときにサブプロシージャーを呼び出す場合に重要になります (例えば、次のような副産物があります)。
     warnings(getNextCustId(OVERDRAWN)) += 1;

式の一般情報については、 を参照してください。 数式の精度規則については、 数値演算の精度規則 を参照してください。 これは、式に除算命令が入っている場合、または EVAL が いずれかの命令拡張を使用する場合には、特に重要です。

図 1. EVAL 命令
 *..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