値として使用される %LEN
この関数を式の右側で使用すると、変数式の桁数または文字数が 戻されます。
数値式の場合、戻り値は、式の精度を表しますが、これは必ずしも、 実際の有効数字の桁数である必要はありません。浮動変数または浮動式の場合、戻り値は 4 または 8 です。 パラメーターが数値リテラルである場合、戻される長さは、 リテラルの桁数です。
文字、図形、または UCS-2 の式の場合、戻り値は、式の値の中の文字数です。可変長の値 (組み込み関数または可変長フィールドから戻り値など) の場合、%LEN によって戻り値は、文字、図形、または UCS-2 の値の現在の長さです。 ただし、%LEN が、定義ステートメント内で名前付き定数の値として、またはキーワードのパラメーター の値として使用される場合、%LEN が戻す値は、その可変長フィールドの最大長です。
それ以外のデータ・タイプの場合はすべて、戻り値は、その値の バイト数です。
図 1. %DECPOS および %LEN の例
*..1....+....2....+....3....+....4....+....5....+....6....+....7...+....
D*Name++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++
D num1 S 7P 2
D NUM1_LEN C %len(num1)
D NUM1_DECPOS C %decpos(num1)
D num2 S 5S 1
D num3 S 5I 0 inz(2)
D chr1 S 10A inz('Toronto ')
D chr2 S 10A inz('Munich ')
D ptr S *
* Numeric expressions:
/FREE
num1 = %len(num1); // 7
num1 = %decpos(num2); // 1
num1 = %len(num1*num2); // 12
num1 = %decpos(num1*num2); // 3
// Character expressions:
num1 = %len(chr1); // 10
num1 = %len(chr1+chr2); // 20
num1 = %len(%trim(chr1)); // 7
num1 = %len(%subst(chr1:1:num3) + ' ' + %trim(chr2));// 9
// %len and %decpos can be useful with other built-in functions:
// Although this division is performed in float, the result is
// converted to the same precision as the result of the eval:
// Note: %LEN and %DECPOS cannot be used directly with %DEC
// and %DECH, but they can be used as named constants
num1 = 27 + %dec (%float(num1)/num3 : NUM1_LEN : NUM1_DECPOS);
// Allocate sufficient space to hold the result of the catenation
// (plus an extra byte for a trailing null character):
num3 = %len (chr1 + chr2) + 1;
ptr = %alloc (num3);
%str (ptr: num3) = chr1 + chr2;
/END-FREE