Chapter 11
Built-In Functions, Subroutines, and Pseudovariables
Endless PL/I for POSIX Systems provides a set of predefined functions, subroutines, and variables called built-in functions, built-in subroutines, and pseudovariables respectively.
Built-in functions are procedures to use wherever an expression is valid.
Built-in subroutines are routines which offer additional capabilities.
Pseudovariables can be used in certain assignment statements in place of ordinary variables.
This chapter describes the built-in functions, subroutines, and pseudovariables that you can use in your PL/I programs.
11.1. Built-In Function Arguments
Built-in functions are similar to operators, and their arguments are similar to operands. Built-in function arguments, if arithmetic, are converted to their derived type before the function reference is evaluated. All evaluations of built-in functions are performed in the result type. The arguments are converted again from the derived type to the result type if necessary. The precision of the result is the greater of the precisions of the two arguments.
For instance, all the mathematical functions return floating-point values; their arguments are converted to floating point (binary or decimal, depending on the base of the argument) before the operation is performed. For example:
DCL J FIXED BINARY(8); FT = ATAN(J,2);
Here the derived type of J and 2 is fixed-point binary. The converted precision of 2 is min(ceil(1/3.32)+1,31), or 2. The result type is FLOAT BINARY(8). Both arguments are then converted to FLOAT BINARY(8), and the ATAN operation is performed.
Note the following restrictions on built-in function arguments:
All arguments of all built-in functions except the array-handling, storage, file-control, and STRING functions must be scalars of arithmetic, string, or pictured data types, as specified for the individual function.
A reference to a built-in function that takes no arguments must still contain the pair of enclosing parentheses, such as NULL(), unless the function's name has been declared with the BUILTIN attribute.
11.2. Conditions Signaled
Built-in functions, like other operations, can signal conditions. The mathematical functions, which are computed in floating point, can signal OVERFLOW and UNDERFLOW under the appropriate conditions. Functions that are computed in fixed point can signal FIXEDOVERFLOW. In general, string and other functions signal ERROR if a result cannot be computed. Refer to Section 8.10, “Condition Handling” for more information.
11.3. Summary of Built-In Functions
The built-in functions are summarized in Table 11.1, “Summary of PL/I Built-In Functions”, according to the following categories:
Arithmetic built-in functions provide information about the properties of arithmetic values, or perform common arithmetic calculations.
Mathematical built-in functions perform standard mathematical calculations in floating point.
String-handling built-in functions process character-string and bit-string values.
Conversion built-in functions convert data from one data type to another.
Condition-handling built-in functions provide information about interrupts caused by signaled conditions.
Array-handling built-in functions provide information about arrays.
Storage control built-in functions return values concerning based variables.
Timekeeping built-in functions return the system date and time of day.
File-control built-in functions return the current line number and page number of a file.
Preprocessor built-in functions are used only at compile time by the embedded preprocessor.
Section 11.4, “Descriptions of Built-In Functions” provides detailed descriptions of the functions listed in Table 11.1, “Summary of PL/I Built-In Functions”.
The abbreviations in the dialect column in Table 11.1, “Summary of PL/I Built-In Functions” are a for ansi, d for dec, and i for ibm dialect.
| Category | Function Reference | Value Returned | Dialect |
|---|---|---|---|
| Arithmetic | ABS(x) | Absolute value of x | a,d,i |
| ADD(x,y,p[,q]) | Value of x+y, with precision p and scale factor q | a,d,i | |
| CEIL(x) | Smallest integer greater than or equal to x | a.d.i | |
| DIVIDE(x,y,p[,q]) | Value of x divided by y, with precision p and scale factor q | a,d,i | |
| FLOOR(x) | Largest integer that is less than or equal to x | a,d,i | |
| MAX(x,y) | Larger of the values x and y | a,d,i | |
| MIN(x,y) | Smaller of the values x and y | a,d,i | |
| MOD(x,y) | Value of x modulo y | a,d,i | |
| MULTIPLY(x,y,p[,q]) | Value of x*y, with precision p and scale factor q | a,d,i | |
| PRECISION(x,p[,q]) | Value of x, with precision p and optional scale factor q | i | |
| ROUND(x,k) | Value of x rounded to k digits | a,d,i | |
| SIGN(x) | -1, 0, or 1 to indicate the sign of x | a,d,i | |
| SUBTRACT(x,y,p[,q]) | Value of x-y, with precision p and scale factor q | a,d | |
| TRUNC(x) | Integer portion of x | a,d,i | |
| Mathematical | ACOS(x) | Arc cosine of x (angle, in radians, whose cosine is x) | a,d,i |
| ASIN(x) | Arc sine of x (angle, in radians, whose sine is x) | a,d,i | |
| ATAN(x) | Arc tangent of x (the angle, in radians, whose tangent is x) | a,d,i | |
| ATAN(x,y) | Arc tangent of x (the angle, in radians, whose sine is x and whose cosine is y) | a,d,i | |
| ATAND(x) | Arc tangent of x (the angle, in degrees, whose tangent is x) | a,d,i | |
| ATAND(x,y) | Arc tangent of x (the angle, in degrees, whose sine is x and whose cosine is y) | a,d,i | |
| ATANH(x) | Hyperbolic arc tangent of x | a,d,i | |
| COS(x) | Cosine of radian angle x | a,d,i | |
| COSD(x) | Cosine of degree angle x | a,d,i | |
| COSH(x) | Hyperbolic cosine of x | a,d,i | |
| ERF(x) | Error function of x | i | |
| ERFC(x) | Complement of the error function of x | i | |
| EXP(x) | Base of the natural logarithm, e, to the power x | a,d,i | |
| LOG(x) | Logarithm of x to the base e | a,d,i | |
| LOG10(x) | Logarithm of x to the base 10 | a,d,i | |
| LOG2(x) | Logarithm of x to the base 2 | a,d,i | |
| SIN(x) | Sine of the radian angle x | a,d,i | |
| SIND(x) | Sine of the degree angle x | a,d,i | |
| SINH(x) | Hyperbolic sine of x | a,d,i | |
| SQRT(x) | Square root of x | a,d,i | |
| TAN(x) | Tangent of the radian angle x | a,d,i | |
| TAND(x) | Tangent of the degree angle x | a,d,i | |
| TANH(x) | Hyperbolic tangent of x | a,d,i | |
| String- Handling | BOOL(x,y,z) | Result of Boolean operation z performed on x and y | a,d,i |
| COLLATE() | ASCII character set | a,d,i | |
| COPY(s,c) | c copies of specified string, s | a,d | |
| EVERY(s) | Boolean value indicating whether every bit in bit string s is '1'B | a,d | |
| HIGH(c) | String of length c of repeated occurrences of the highest character in the collating sequence | a,d,i | |
| INDEX(s,c[,p]) | Position of the character string c within the string s, starting at position p | a,d,i | |
| LENGTH(s) | Number of characters or bits in the string s | a,d,i | |
| LOW(c) | String of length c of repeated occurrences of the lowest character in the collating sequence | a,d,i | |
| MAXLENGTH(s) | Maximum length of varying string s | a,d | |
| REPEAT(x,y) | x concatenated to itself y times | i | |
| REVERSE(s) | Reverse of the source character string or bit string | a,d | |
| SEARCH(s,c[,p]) | Position of the first character in s, starting at position p, that is found in c | a,d | |
| SOME(s) | Boolean value indicating whether at least one bit in bit string s is '1'B | a,d | |
| STRING(s) | Concatenation of values in array or structure s | a,d,i | |
| SUBSTR(s,i[,j]) | Part of string s beginning at i for j characters | a,d,i | |
| TRANSLATE(s,c[,d]) | String s with substitutions defined in c and d | a,d,i | |
| TRIM(s[,e,f]) | String s with all characters in e removed from the left, and all characters in f removed from the right. If you omit either e or f or both, blanks are removed from that side. | a,d | |
| VERIFY(s,c[,p]) | Position of the first character in s, starting at position p, which is not found in c | a,d,i | |
| Conversion | BINARY(x[,p[,q]]) | Binary value of x with precision p and scale factor q | a,d,i |
| BIT(s[,l]) | Value of s converted to a bit string of length l | a,d,i | |
| BYTE(x) | ASCII character represented by the integer x | a,d | |
| CHARACTER(s[,l]) | Value of s converted to a character string of length l | a,d,i | |
| DECIMAL(x[,p[,q]]) | Decimal value of x | a,d,i | |
| DECODE(c,r) | Fixed binary value of the character string c converted to a base r number | a,d | |
| ENCODE(i,r) | Character string representing the base r number that is equivalent to the fixed binary expression i | a,d | |
| FIXED(x,p[,q]) | Fixed arithmetic value of x | a,d,i | |
| FLOAT(x,p) | Floating arithmetic value of x | a,d,i | |
| INT(x[,p[,l]]) | Signed integer value of variable x, located at position p with length l | a,d | |
| POSINT(x[,p[,l]]) | Unsigned integer value of variable x, located at position p with length l | a,d | |
| RANK(c) | Integer representation of the ASCII character c | a,d | |
| UNSPEC(x[,p[,l]]) | Internal coded form of x, located at position p with length l | a,d,i | |
| Condition- Handling | ONCHAR() | Character that caused the CONVERSION condition to be raised | a,d,i |
| ONCODE() | Error code of the most recent run-time error | a,d,i | |
| ONFILE() | Name of file constant for which the most recent ENDFILE, ENDPAGE, KEY, or UNDEFINEDFILE condition was signaled | a,d,i | |
| ONKEY() | Value of key that caused KEY condition | a,d,i | |
| ONLOC | Name of the entry point used for the invocation of the procedure where the condition was signaled | i | |
| ONSOURCE() | Field containing the ONCHAR character when the CONVERSION condition was raised | a,d,i | |
| Array- Handling | ALL(x) | Boolean AND of all array elements | i |
| ANY(x) | Boolean OR of all array elements | i | |
| DIMENSION(x[,n]) | Extent of the nth dimension of x | a,d,i | |
| HBOUND(x[,n]) | Higher bound of the nth dimension of x | a,d,i | |
| LBOUND(x[,n]) | Lower bound of the nth dimension of x | a,d,i | |
| PROD(x) | Arithmetic product of all the elements in x | a,d,i | |
| SUM(x) | Arithmetic sum of all the elements in x | a,d,i | |
| Storage | ADDR(x) | Pointer identifying the storage referenced by x | a,d,i |
| ALLOCATION(x) | Number of existing generations for controlled variable x | a,d,i | |
| BYTESIZE(x) | Number of bytes allocated to variable x; same as SIZE function | a,d | |
| CURRENTSTORAGE(x) | Number of bytes allocated to variable x, precision fixed bin(15) | i | |
| EMPTY() | An empty area value | a,d,i | |
| NULL() | A null pointer value | a,d,i | |
| POINTER(o,a) | A pointer to the location at offset o within area a | a,d,i | |
| SIZE(x) | Number of bytes allocated to variable x, precision fixed bin(31) | a,d | |
| STORAGE(x) | Number of bytes allocated to variable x, precision fixed bin(31) | i | |
| Timekeeping | DATE() | System date in the form YYMMDD | a,d,i |
| DATETIME() | System date and time in the form CCYYMMDDHHMMSSXX | a,d | |
| TIME() | System time of day in the form HHMMSSXX | a,d,i | |
| File Control | LINENO(x) | Line number of the print file identified by x | a,d,i |
| PAGENO(x) | Page number of the print file identified by x | a,d,i | |
| Misc. | PLIRETV | PL/I return code, precision fixed bin(15) | i |
| Preprocessor | ABS(x) | Absolute value of x | a,d,i |
| BYTE(x) | ASCII character represented by integer x | a,d,i | |
| COPY(s,c) | c copies of specified string s | a,d,i | |
| DATE() | Compilation date in the form YYMMDD | a,d,i | |
| DATETIME() | System date and time in the form CCYYMMDDHHMMSSXX | a,d,i | |
| DECODE(c,r) | Fixed binary value of the character string c converted to a base r number | a,d,i | |
| ENCODE(i,r) | Character string representing the base r number that is equivalent to the fixed binary expression i | a,d,i | |
| ERROR() | Count of user-generated diagnostic error messages | a,d,i | |
| INDEX(s,c[,p]) | Position of the character string c within the string s, starting at position p | a,d,i | |
| INFORM() | Count of user-generated diagnostic informational messages | a,d,i | |
| LENGTH(s) | Number of characters or bits in the string s | a,d,i | |
| LINE() | Line number in source program that contains the end of the specified preprocessor statement | a,d,i | |
| MAX(x,y) | Larger of the values x and y | a,d,i | |
| MIN(x,y) | Smaller of the values x and y | a,d,i | |
| MOD(x,y) | Value of x modulo y | a,d,i | |
| RANK(c) | Integer representation of the ASCII character c | a,d,i | |
| REVERSE(s) | Reverse of the source character string or bit string | a,d,i | |
| SEARCH(s,c[,p]) | Position of the first character in s, starting at position p, that is found in c | a,d,i | |
| SIGN(x) | -1,0, or 1 to indicate the sign of x | a,d,i | |
| SUBSTR(s,i[,j]) | Part of string s beginning at i for j characters | a,d,i | |
| TIME() | Compilation time of the day in the form HHMMSSXX | a,d,i | |
| TRANSLATE(s,c[,d]) | String s with substitutions defined in c and d | a,d,i | |
| TRIM(s[,e,f]) | String s with all characters in e removed from the left and all characters in f removed from the right If you omit either e or f or both, blanks are removed from that side | a,d,i | |
| VARIANT() | String result representing the value of -T command
option | a,d,i | |
| VERIFY(s,c[,p]) | Position of the first character in s, starting at position p, which is not found in c | a,d,i | |
| WARN() | Count of user-generated diagnostic warning messages | a,d,i |
11.4. Descriptions of Built-In Functions
This section presents the built-in functions in alphabetical order.
11.4.1. ABS
Dialects - ansi, dec, ibm
The ABS built-in function returns the absolute value of an arithmetic expression x. Its format is:
ABS(x)Examples
A = 3.567; Y = ABS(A); /* Y = +3.567 */ A = -3.567; Y = ABS(A); /* Y = +3.567 */ ROOT = SQRT (ABS(TEMP));
The last example shows a common use for the ABS built-in function: to ensure that an expression has a positive value before it is used as an argument to the square root (SQRT) built-in function.
11.4.2. ACOS
Dialects - ansi, dec, ibm
The ACOS built-in function returns a floating-point value that is the arc (inverse) cosine of an arithmetic expression x. The arc cosine is computed in floating point. The returned value is an angle w such that:
0 < = w <=p
The absolute value of x, after its conversion to floating point, must be less than or equal to 1. The format of the function is:
ACOS(x)Dialects - ansi, dec, ibm
The ADD built-in function returns the sum of two arithmetic expressions x and y, with a specified precision p and an optionally specified scale factor q. The format of the function is:
ADD(x,y,p[,q])
pAn unsigned integer constant greater than zero and less than or equal to the maximum precision of the result type, which is 31for fixed-point data, 15 for floating-point decimal data, and 53 for floating-point binary data.
qAn integer constant less than or equal to the specified precision. The scale factor can be optionally signed when used in fixed-point binary addition. The scale factor for fixed-point binary must be in the range -31 to p. The scale factor for fixed-point decimal data must be in the range 0 to p. If you omit q, the default value is zero. Do not use a scale factor for floating-point arithmetic.
Expressions x and y are converted to their derived type before the addition is performed. For example:
ADDBIF: PROCEDURE OPTIONS (MAIN); DECLARE X FIXED DECIMAL (8,3), Y FIXED DECIMAL (8,3), Z FIXED DECIMAL (9,3); X=9500.374; Y=2278.897; Z = ADD (X,Y,9,3); PUT SKIP LIST ('TOTAL =',Z); END;
This program prints the following:
TOTAL = 11779.271
11.4.4. ADDR
Dialects - ansi, dec, ibm
The ADDR built-in function returns a pointer to storage denoted by a specified variable. The variable reference must be addressable. The format of the function is:
ADDR(reference)If the reference is to a parameter (or any element or member of a parameter), the pointer value obtained must not be used after return from the parameter's procedure invocation. (This could occur, for example, if the pointer were saved in a static variable or returned as a function value.)
11.4.5. ALL
Dialects - ibm
The ALL built-in function takes an array as an argument and returns a bit string the length of the longest element of the referenced array. The returned string contains a one in every position where the equivalent positions in all the elements of the array exist and are ones. The format of the ALL built-in function is:
ALL(array-variable)Dialects - ansi, dec, ibm
The ALLOCATION built-in function returns a fixed-point binary integer that is the number of existing generations of a specified controlled variable. If no generations of the specified variable exist, the function returns zero. The format of the function is:
{ ALLOCATION } (reference)
{ ALLOCN }referenceThe name of a controlled variable.
Examples
DECLARE INPUT CHARACTER(10) CONTROLLED, A CHARACTER(3) VARYING; ⋮ DO UNTIL (INPUT = 'QUIT'); ALLOCATE INPUT; GET LIST(INPUT); ⋮ END; A = ALLOCATION(INPUT); PUT SKIP LIST('Generations = ';A);
This example uses the ALLOCATION built-in function to return the number of generations of the controlled variable INPUT. The example illustrates how input in an interactive program can be stored on a stack for future use.
11.4.7. ANY
Dialects - ibm
The ANY built-in function takes an array as an argument and returns a bit string the length of the longest element of the referenced array. The returned string contains a one in every position where the equivalent positions in any of the elements of the array exist and is a one. The format of the ANY built-in function is:
ANY(array-variable)11.4.8. ASIN
Dialects - ansi, dec, ibm
The ASIN built-in function returns a floating-point value that is the arc (inverse) sine of an arithmetic expression x. The arc sine is computed in floating point. The returned value is an angle w such that:
-p/2 < = w<= p/2
The absolute value of x, after its conversion to floating point, must be less than or equal to 1. The format of the function is:
ASIN(x)11.4.9. ATAN
Dialects - ansi, dec, ibm
The ATAN built-in function returns a floating-point value that is the arc tangent of an arithmetic expression y or an arc tangent computed from two arithmetic expressions y and x. The arc tangent is computed in floating point. If two arguments are supplied, they must both have nonzero values after they have been converted to floating point.
The format of the function is:
ATAN(y[,x])
Returned Values
The returned value represents an angle in radians.
If x is omitted, the returned value v equals arc tangent(s), such that:
-p/2 < v< p/2
Here, s is the value of expression y after its conversion to floating point.
If x is present, the returned value v equals arc tangent(s/r), such that if s >= 0 then 0 <= v <= p and if s < 0 then -p < v < 0, where s and r are, respectively, the values of expressions y and x after their conversion to floating point.
11.4.10. ATAND
Dialects - ansi, dec, ibm
The ATAND built-in function returns a floating-point value that is the arc tangent of a single arithmetic expression y or an arc tangent computed from two arithmetic expressions y and x. The arc tangent is computed in floating point. If two arguments are supplied, they must both have nonzero values after their conversion to floating point.
The format of the function is:
ATAND(y[,x])
Returned Value
The floating-point value returned (which represents an angle in degrees) equals:
ATAN(y,x)*180/p
11.4.11. ATANH
Dialects - ansi, dec, ibm
The BINARY built-in function converts an arithmetic or string expression x to its binary representation, with an optionally specified precision p and scale factor q. The returned value is either fixed- or floating-point binary, depending on whether x is a fixed- or floating-point expression.
The format of the function is:
{ BINARY }(x[,p[,q]])
{ BIN }pThe precision p, if specified, must be an integer constant greater than zero and less than or equal to the maximum precision of the result type—31if fixed-point binary and 53 if floating-point binary.
The precision p must be specified if x is a fixed-point value with fractional digits.
qThe scale factor q, if specified, must be an integer constant less than or equal to the specified precision and in the range -31 to 31.
Returned Value
The result type is fixed- or floating-point binary, depending on whether the argument x is a fixed- or floating-point expression. (If the argument is a bit- or character-string expression, the result type is fixed-point binary.)
The argument x is converted to the result type, giving a value v, following the PL/I rules for conversion.
The returned value is the value v, with precision p, and scale factor q. If p is omitted (integer and floating-point arguments only), the precision of the returned value is the converted precision of x. FIXEDOVERFLOW, OVERFLOW, or UNDERFLOW is signaled if appropriate.
11.4.13. BIT
Dialects - ansi, dec, ibm
The BIT built-in function converts an arithmetic or string expression x to a bit string of an optionally specified length. If x is a string expression, it must consist of 0s and 1s. If the length is specified, it must be a nonnegative integer. If the length is omitted, the returned value has a length determined by the PL/I rules for conversion to bit strings.
The format of the function is:
BIT(x[,length])
11.4.14. BOOL
Dialects - ansi, dec, ibm
The BOOL built-in function performs a Boolean operation on two bit-string arguments and returns the result as a bit string with the length of the longer argument.
The format of the function is:
BOOL(string-1,string-2,operation-string)
string-1A bit-string expression of any length.
string-2A bit-string expression of any length.
operation-stringA bit-string expression that is converted to length 4. Each bit in the operation string specifies the result of comparing two corresponding bits in
string-1andstring-2. Specify bit positions in the operation string from left to right to define the operation, as in the following truth table:String-1 Bit String-2 Bit Result of Boolean Operation 0 0 Bit 1 of operation string 0 1 Bit 2 of operation string 1 0 Bit 3 of operation string 1 1 Bit 4 of operation string Thus, an AND operation, for instance, would be specified by the
operation-string'0001'B.If
string-1andstring-2are of different lengths, the shorter is extended on the right with zeros to the length of the longer.
Examples
X = '101010'B; Y = '110011'B; CHECK = BOOL (X,Y,'0110'B);
The operation string is '0110'B, which defines an EXCLUSIVE OR operation. The operation is performed as follows on the corresponding bits in the strings X and Y: The leftmost bit in X is 1 and the leftmost bit in Y is 1. The truth table above specifies that when the two corresponding bits in the two strings are both 1, then bit 4 of the operation string will be the result; in this case, bit 4 of the operation string '0110'B is 0. Thus, 0 is the first bit of the value to be returned. The second bit of X is 0 and of Y is 1. The truth table specifies that when the bit in the first string is 0 and in the second string is 1, the result will be bit 2 of the operation string. Here, bit 2 of the operation string '0110'B is 1, and so 1 is the second bit of the value to be returned. The operation continues in this manner with each two corresponding bits in the strings. The value returned is '011001'B.
Figure 11.1, “Example of the BOOL Built-In Function” illustrates this example.
11.4.15. BYTE
Dialects - ansi, dec
The BYTE built-in function returns the ASCII character whose ASCII code is the integer x; x must not be negative. The returned value is a character equivalent to BYTE(y), where y equals x modulo 256. The format of the function is:
BYTE(x)Examples
DECLARE CHAR CHARACTER(1); CHAR = BYTE(65); /* CHAR = 'A' */ CHAR = BYTE(32); /* CHAR = ' ' (space) */
11.4.16. BYTESIZE
Dialects - ansi, dec
This function is the same as the SIZE function. See Section 11.4.81, “SIZE”.
11.4.17. CEIL
Dialects - ansi, dec, ibm
The CEIL function returns the smallest integer that is greater than or equal to an arithmetic expression x. Its format is:
CEIL(x)Returned Value
If x is a floating-point expression, a floating-point value is returned with the same precision as x. If x is a fixed-point expression, the returned value is a fixed-point value of the same base as x and with:
precision = min(31,p -q + 1)
scale factor = 0
Here, p and q are the precision and scale factor of x.
Examples
A = 4.3; Y = CEIL(A); /* Y = 5 */ A = -4.3; Y = CEIL(A); /* Y = -4 */
11.4.18. CHARACTER
Dialects - ansi, dec, ibm
The CHARACTER built-in function converts an arithmetic or string expression x to a character string of an optionally specified length. If the length is specified, it must be a nonnegative integer. If the length is omitted, the length of the returned value is determined by the PL/I rules for conversion to character strings. The format of the function is:
{ CHARACTER } (x[,length])
{ CHAR }Examples
CHAR: PROCEDURE OPTIONS(MAIN); DECLARE EXPRES FIXED DECIMAL(7,5); DECLARE OUTPUT PRINT FILE; EXPRES = 12.34567; OPEN FILE(OUTPUT) TITLE('CHAR2.OUT'); PUT SKIP FILE(OUTPUT) LIST('No length argument: ',CHARACTER(EXPRES)); PUT SKIP FILE(OUTPUT) LIST('Length = 4: ',CHARACTER(EXPRES,4)); END CHAR;
The program CHAR produces the following output:
No length argument: 12.34567 Length = 4: 12
In the first PUT LIST statement, CHARACTER has only one argument, so the entire string is written out. The string '12.34567' is actually preceded by two spaces; this is the case with any nonnegative number converted to a character string. In the second PUT LIST statement, CHARACTER has a length argument of 4, so the first four characters of the converted string are written out as ' 12'.
11.4.19. COLLATE
11.4.20. COPY
Dialects - ansi, dec
The COPY built-in function copies a given string a specified number of times and concatenates the result into a single string. Its format is:
COPY(string,count)
stringAny bit- or character-string expression. If the expression is a bit string, the result is a bit string. Otherwise, the result is a character string.
countAny expression that yields a nonnegative integer. The specified count controls the number of copies of the string that are concatenated, as follows:
Value of Count String Returned 0 A null string 1 The string argument n Concatenated copies of the string argument
Examples
COPY('12',3)
This function reference returns the character-string value '121212'.
11.4.21. COS
11.4.22. COSD
11.4.23. COSH
11.4.24. CURRENTSTORAGE
Dialects - ibm
The CURRENTSTORAGE built-in function returns a fixed binary (15) value that is the implementation-defined storage, in bytes, required by the specified variable. The format of the function is:
CURRENTSTORAGE(reference)referenceThe name of a variable known to this block except:
A variable that is not in connected storage.
An unaligned fixed-length BASED, DEFINED, subscripted, parameter, or structure element.
A minor structure with a first or last element that is an unaligned fixed-length bit string, unless that element is the first or last element of the containing major structure.
A major structure with the BASED, DEFINED, or parameter attribute with a first or last element that is an unaligned fixed-length bit string.
Returned Value
The returned value is a fixed bin(15) value containing the variable's actual storage size in bytes. This is the number of bytes that would be transmitted if you wrote the variable to a record file defined with the ENVIRONMENT(SCALARVARYING) option.
Some variables can have varying sizes. If the referenced variable is a varying-length string, the returned value includes the two bytes of storage allocated by PL/I to contain the current length of the string (the length-prefix) and the currently-used bytes. It does not include any unused bytes at the end of the string.
If the referenced variable is a scalar area, the returned value includes the storage used by the area control bytes and the current extent. It does not include any unused bytes at the end of the area.
However, if the referenced variable is an aggregate that contains areas or varying-length strings, the returned value includes the storage used by any control bytes and length prefixes plus the maximum lengths of the strings and the maximum sizes of the areas. This rule has one exception. If the referenced variable is a structure and the last element is a non-dimensioned area, the returned value contains only the current extent of that area, not the unused bytes at the end of the area.
Dialects - ansi, dec, ibm
The DATE built-in function returns a 6-character string in the form yymmdd, where:
| yy | Is the current year (00-99) |
| mm | Is the current month (01-12) |
| dd | Is the current day of the month (01-31) |
Its format is:
DATE()
The date returned is the run-time date. However, if DATE is used as a preprocessor built-in function, the date returned is the compile-time date.
Dialects - ansi, dec
The DATETIME built-in function returns a 16-character string in the form ccyymmddhhmmssxx, where:
| cc | Is the current century (00-99) |
| yy | Is the current year (00-99) |
| mm | Is the current month (01-12) |
| dd | Is the current day of the month (01-31) |
| hh | Is the current hour (00-23) |
| mm | Is the minutes (00-59) |
| ss | Is the seconds (00-59) |
| xx | Is the hundredths of seconds (00-99) |
The format of the function is:
DATETIME()
The date and time returned is the run-time date and time. However, if DATETIME is used as a preprocessor built-in function, the date and time returned is the compile-time date and time.
Note that the DATETIME function is identical to the century concatenated with DATE() and TIME().
11.4.27. DECIMAL
Dialects - ansi, dec, ibm
The DECIMAL built-in function converts an arithmetic or string expression x to a decimal value of an optionally specified precision p and scale factor q.
P and q, if specified, must be integer constants. P must be greater than zero and less than or equal to the maximum precision for the result type (31 for fixed-point, 34 for floating- point). If q is specified, x must be a fixed-point expression and p must also be specified; if q is omitted or has a negative value, the scale factor of the result is zero.
The format of the function is:
{ DECIMAL }(x[,p[,q]])
{ DEC }Returned Value
The result type is fixed-point or floating-point decimal, depending on whether x is a fixed- or floating-point expression. (If x is a bit- or character-string expression, the result type is fixed-point decimal.)
The expression x is converted to a value v of the result type, following the PL/I rules for conversion. The returned value is v with precision p and scale factor q. If p and q are omitted, they are the converted precision and scale factor of x. FIXEDOVERFLOW, UNDERFLOW, or OVERFLOW is signaled if appropriate.
Dialects - ansi, dec
The DECODE built-in function converts a character string representing a number to a fixed binary number. It takes two arguments: a character string and an integer expression It converts the string to an unsigned, base r integer, where r is the specified radix. The function returns a FIXED BINARY(31,0) number representing the base ten equivalent of the string.
The format of the function is:
DECODE(character-expression,radix-expression)
character-expressionA character-string constant or variable whose component characters can be any of the digits from `0' through `9', from `a' through `f', and from `A' through `F'. The digits must be within the range of digits valid for the base specified in the
radix-expression.radix-expressionAn expression evaluating to any integer from 2 through 16.
Examples
DECLARE (X,Y) FIXED BINARY; X = DECODE('1010',2); Y = DECODE('f0',16);
The fixed binary variables X and Y are given the values 10 and 240, respectively.
11.4.29. DIMENSION
Dialects - ansi, dec, ibm
The DIMENSION built-in function returns a fixed-point binary integer that is the number of elements in an array dimension. Its format is:
{ DIMENSION } (reference [,dimension])
{ DIM }referenceA reference to an array variable.
dimensionAn integer constant specifying the dimension of the array for which the extent is to be determined. If the dimension is not specified, the dimension parameter defaults to 1. Thus, DIMENSION(A) is equivalent to DIMENSION(A,1).
Examples
INIT: PROCEDURE (ARRAY); DECLARE ARRAY(*) FIXED, I FIXED; DO I = 1 TO DIM(ARRAY); ARRAY(I) = I; END;
This procedure is passed a one-dimensional array of an unknown extent. The DIMENSION built-in function is used as the end value in a controlled DO statement. This DO-group assigns integral values to each element of the array ARRAY so that the first element has the value 1, the second element has the value 2, and so on to the last element of the array. (Because the array is one-dimensional, the optional second parameter is omitted and defaults to 1.)
11.4.30. DIVIDE
Dialects - ansi, dec, ibm
The DIVIDE built-in function divides an arithmetic expression x by an arithmetic expression y and returns the quotient with a specified precision p and an optionally specified scale factor q. The scale factor q must be an integer following these rules:
If either x or y is fixed binary, q must be in the range -31 through 31.
If both x and y are fixed decimal, q must not be negative.
If either x or y is floating point, q must be zero.
If q is omitted, it is assumed to be zero.
The expressions x and y are converted to their derived types before the division is performed. If y is zero after this conversion, the ZERODIVIDE condition is signaled. The quotient has the derived type of the two arguments.
The format of the function is:
DIVIDE(x,y,p[,q])
11.4.31. EMPTY
Dialects - ansi, dec, ibm
The EMPTY built-in function returns an empty area value for use in initializing areas. Its format is:
EMPTY()
The EMPTY built-in function is useful in initializing the contents of an area. Note that an area value must be assigned to an area before the area is used.
The following is an example of its use in a declaration:
DECLARE A AREA(1024) STATIC INITIAL(EMPTY());
Dialects - ansi, dec
The ENCODE built-in function converts a decimal integer to a character string. It converts the decimal integer (stored as a FIXED BINARY(31,0) number) to a base r number, where r is the radix you specify, and returns the resulting number as a character string. The function takes two arguments: a decimal integer and a radix; the radix is an integer in the range 2 through 16.
The format of the function is:
ENCODE(integer-expression,radix-expression)
integer-expressionAn expression evaluating to a fixed binary number representing a decimal integer. Whether signed or not, this integer is treated by the function as unsigned.
radix-expressionAn expression that evaluates to any integer from 2 through 16.
Examples
DECLARE (X,Y) CHARACTER(5) VARYING; X = ENCODE(53,8); Y = ENCODE(10,2);
The character-string variable X is assigned the value '65', which is the character equivalent of the octal number 65, which is the equivalent of the decimal number 53. The character-string variable Y is assigned the value '1010', which is the character equivalent of the binary number 1010, which is the equivalent of the decimal number 10.
11.4.33. ERF
Dialects - ibm
The ERF built-in function returns a floating-point value that is an approximation of the error function of x. The format of the function is:
ERF(real-expression)The returned value has the base and precision of the real-expression, and the following
value:
11.4.34. ERFC
Dialects - ibm
The ERFC built-in function returns a floating-point value that is an approximation of the complement of the error function of x. The format of the function is:
ERFC(real-expression)The returned value has the base and precision of the real-expression, and the following
value:
1 - ERF(real-expression)Dialects - ansi, dec
The ERROR preprocessor built-in function returns the number of preprocessor diagnostic error messages issued during compilation up to that particular point in the source program. The format for the ERROR built-in function is:
ERROR();
The function returns a fixed-point result representing the number of compile-time warning messages that were issued up until the point at which the built-in function was encountered.
Dialects - ansi, dec
The EVERY built-in function determines whether every bit in a bit string is '1'B. In other words, it performs a logical AND operation on the elements of the bit string. The format of the function is:
EVERY(bit-string)The function returns the value '1'B if all bits in the bit-string argument are '1'B. It returns '0'B if one or more bits in the argument are '0'B or if the argument is the null bit string.
11.4.37. EXP
11.4.38. FIXED
Dialects - ansi, dec, ibm
The FIXED built-in function converts an arithmetic or string expression x to a fixed-point arithmetic value with a specified precision p and, optionally, a scale factor q.
The format of the function is:
FIXED(x,p[,q])
pThe number of bits used to represent the arithmetic value. The precision must be greater than zero and less than or equal to 31.
qAn integer in the range 0 through 31 for decimal data, and in the range -31 through 31 for binary data. If q is omitted, it is assumed to be zero. The scale factor q must be less than or equal to the specified precision.
Returned Value
The result type is fixed-point binary or decimal, depending on whether x is binary or decimal. (If x is a bit string, the result type is fixed-point binary; if x is a character string, the result type is fixed-point decimal.)
The expression x is converted to a value v of the result type, following the PL/I rules. The returned value is v with precision p and scale factor q. If q is omitted, the returned value has the converted precision of x and a scale factor of zero. FIXEDOVERFLOW is signaled if appropriate.
11.4.39. FLOAT
Dialects - ansi, dec, ibm
The FLOAT built-in function converts a string or arithmetic expression x to floating point, with a specified precision p. The precision p must be an integer constant that is greater than zero and less than or equal to the maximum precision of the result type, which is 15 for floating-point decimal data and 53 for floating-point binary data
If x is a character string, it can contain any series of characters that describes a valid arithmetic constant. That is, the character string can contain any of the numeric digits 0 through 9, a plus (+) or minus (-) sign, a decimal point (.), and the letter E. If the character string contains any invalid characters, the CONVERSION condition is signaled. The format of the FLOAT function is:
FLOAT(x,p)
Returned Value
The result type is floating-point binary or decimal, depending on whether x is a binary or decimal expression. (If x is a bit-string expression, the result type is floating-point binary; if x is a character-string expression, the result type is floating-point decimal.)
The expression x is converted to a value of the result type, following the PL/I conversion rules, and of the specified precision. UNDERFLOW or OVERFLOW is signaled if appropriate.
11.4.40. FLOOR
Dialects - ansi, dec, ibm
The FLOOR built-in function returns the largest integer that is less than or equal to an arithmetic expression x. The format is:
FLOOR(x)Returned Value
If x is a floating-point expression, the returned value is a floating-point value. If x is a fixed-point expression, the returned value is a fixed-point value with the same base as x and with the following attributes:
precision = min(31,p - q + 1)
scale factor = 0
Here, p and q are the precision and scale factor of x.
For example:
FLOOR_DEMO: PROC OPTIONS(MAIN); PUT LIST (FLOOR(3)); PUT LIST (FLOOR(-3.323)); PUT LIST (FLOOR(3.456E9)); END;
This program prints the following values:
3 -4 3.456E+09
11.4.41. HBOUND
Dialects - ansi, dec, ibm
The HBOUND built-in function returns a fixed-point binary integer that is the upper bound of an array dimension. The format is:
HBOUND(reference[,dimension])
referenceA reference to an array variable.
dimensionAn integer constant indicating a dimension of the specified array. If the dimension is not specified, the dimension parameter defaults to 1. Thus, HBOUND(A) is equivalent to HBOUND(A,1).
11.4.42. HIGH
Dialects - ansi, dec, ibm
The HIGH built-in function returns a string of specified length that consists of repeated appearances of the highest character in the collating sequence. The format is:
HIGH(length)lengthThe specified length of the returned string. The (maximum length of the returned string is 32767 characters.
Returned Value
The string returned is of the length specified. The rank of the highest character that can appear in the collating sequence for PL/I is ASCII DEL (value 255).
11.4.43. INDEX
Dialects - ansi, dec, ibm
The INDEX built-in function returns a fixed-point binary integer that indicates the position of the leftmost occurrence of a specified substring within a string. If the substring is not found, or if the length of either argument is zero, the INDEX function returns zero. This function is case-sensitive.
The format of the function is:
INDEX(string,substring[,starting-position])
stringThe string to be searched for the given substring. It can be either a character-string or a bit-string expression.
substringThe substring to be located. It must have the same string data type as the string argument.
starting-positionA positive integer in the range 1 to n+1, where n is the length of the string. It specifies the leftmost position from which the search is to begin. (By default, the search begins at the left end of the string.)
Examples
DECLARE RESULT FIXED BINARY(31); RESULT = INDEX ('ABCDEF','DEF');
RESULT is given the value 4 because the substring `DEF' begins at the fourth position in `ABCDEF'.
RESULT = INDEX('SHARP FORTUNE','R');
RESULT is given the value 4 because the leftmost occurrence of `R' is at the fourth position in `SHARP FORTUNE'.
RESULT = INDEX('SHARP FORTUNE','R',5);
The optional
starting-positionparameter specifies that the search begins at the fifth position of `SHARP FORTUNE'. Thus, RESULT is given the value 9: the first R is ignored, so the first recognized occurrence of `R' is found in the ninth position.RESULT = INDEX('0000101100001011','1011');
RESULT is given the value 5 because the leftmost occurrence of '1011' is at the fifth position in '0000101100001011'.
NEW_STRING = '315-54-3159'; IF INDEX(NEW_STRING,'-')=4 THEN PUT LIST('SOCIAL SECURITY NUMBER');
The INDEX function is used to determine whether or not a string is a Social Security number. The function finds the location of the first hyphen in the string.
Dialects - ansi, dec
The INFORM preprocessor built-in function returns the number of diagnostic informational messages issued during compilation up to that point in the source program. The format for the INFORM built-in function is:
INFORM();
The function returns a FIXED result representing the number of compile-time warning messages that were issued up until the INFORM built-in function was encountered.
11.4.45. INT
Dialects - ansi, dec
The INT built-in function treats specified storage as a signed integer, and returns the value of the integer. The format is:
INT(expression[,position[,length]])
expressionA scalar expression or reference to connected storage. This reference cannot be an array, structure, or named constant. If position and length are not specified, the length of the referenced storage must not exceed 32 bits. If it exceeds 32 bits, a fatal run-time error results.
positionA positive integer value that denotes the position of the first bit in the field. If omitted, position defaults to 1, signifying the first bit of storage denoted by the expression. If specified, position must satisfy the following condition:
1 <= position <= size(expression)
Here, size(expression) is the length in bits of the storage denoted by expression. A position equal to size(expression) implies a zero-length field.
lengthAn integer value in the range 0 through 32 that specifies the length of the field. If omitted, length is the number of bits from the bit denoted by position through the end of the storage denoted by expression. If specified, length must satisfy the following condition:
0 <= length <= size(expression) - position
Here, size(expression) is the length in bits of the storage denoted by expression.
Returned Value
The value returned by INT is of the type FIXED BINARY (31). If the field has a length of zero, INT returns zero.
Examples
The following example shows the use of the INT built-in function to interpret the storage occupied by a bit string as an integer:
B16 = '0000000000001101'B; /* 16-bit string */ I = BIN(B16); /* I = 13 */ I = INT(B16); /* I = -20480 */ B64 = '5076ABCD00000000'B4; /* 64-bit string */ I = INT(B64,1,32); /* First 32 bits; I = -1277858294 */ I = INT(B64,33); /* Second 32 bits; I = 0 */ I = INT(B64); /* Field too large, run-time error */
Notice that, unlike the BIN built-in function, the INT built-in function performs no conversion. It simply treats the contents of the designated storage as a signed integer. Therefore, the value returned by INT depends on the data type (and therefore the internal representation) of the variable occupying the storage. For example:
INTEXM: PROCEDURE OPTIONS (MAIN); DECLARE D FIXED DECIMAL (3,2), C CHARACTER (4), F FLOAT; D = 2.54; C = '2.54'; F = 2.54; PUT SKIP LIST ( INT(D), INT(C), INT (F) ); END;
The output of this example is:
19493 875900466 -1889779422
11.4.46. LBOUND
Dialects - ansi, dec, ibm
The LBOUND built-in function returns a fixed-point binary integer that is the lower bound of an array dimension. The format is:
LBOUND(reference[,dimension])
referenceA reference to an array variable.
dimensionAn integer constant indicating the dimension of the specified array. If the dimension is not specified, the dimension parameter defaults to 1. Thus, LBOUND(A) is equivalent to LBOUND(A,1).
11.4.47. LENGTH
Dialects - ansi, dec, ibm
The LENGTH built-in function returns a fixed-point binary integer that is the number of characters or the number of bits in a specified character- or bit-string expression. If the string is a varying-length character string, the function returns its current length. (To determine the maximum length of a varying-length character string, use the MAXLENGTH built-in function.)
The format of the function is:
LENGTH(string)Dialects - ansi, dec, ibm
The LINE preprocessor built-in function returns the line number of the source program text containing the end of the preprocessor statement that calls the LINE built-in function.
The format of the function within a preprocessor expression is:
LINE()
11.4.49. LINENO
Dialects - ansi, dec, ibm
The LINENO built-in function returns a FIXED BINARY(15) integer that is the current line number of the referenced print file. The format is:
LINENO(reference)If the referenced print file is closed, the returned value is the last value from the previous opening. If the file was never opened, the returned value is zero.
11.4.50. LOG
Dialects - ansi, dec, ibm
The LOG built-in function returns a floating-point value that is the base e (natural) logarithm of an arithmetic expression x. The computation is performed in floating point. The expression x must be greater than zero after its conversion to floating point.
The format of the function is:
LOG(x)11.4.51. LOG10
Dialects - ansi, dec, ibm
The LOG10 built-in function returns a floating-point value that is the base 10 logarithm of arithmetic expression x. The computation is performed in floating point. The expression x must be greater than zero after its conversion to floating point.
The format of the function is:
LOG10(x)11.4.52. LOG2
Dialects - ansi, dec, ibm
The LOG2 built-in function returns a floating-point value that is the base 2 logarithm of an arithmetic expression x. The computation is performed in floating point. The expression x must be greater than zero after its conversion to floating point.
The format of the function is:
LOG2(x)Dialects - ansi, dec, ibm
The LOW built-in function returns a string of specified length that consists of repeated appearances of the lowest character in the collating sequence. The format is:
LOW(length)lengthThe specified length of the returned string. The maximum length permitted is 32767 characters.
Returned String
The string returned is of the length specified. The rank of the lowest character that can appear in the collating sequence for PL/I is ASCII NUL (value 0).
11.4.54. MAX
Dialects - ansi, dec, ibm
The MAX built-in function returns the larger of two arithmetic expressions x and y. The format of the function is:
MAX(x,y)
Returned Value
The expressions x and y are converted to their derived type before the operation is performed (for a discussion of derived types see Section 6.4.2, “Derived Data Types for Arithmetic Operations”. If the derived type is floating point, the value returned is also floating point, with the larger precision of the two converted arguments. If the derived type is fixed point, the returned value is a fixed-point value with the base of the derived type. The value has the following attributes:
precision = min(31,max(px - qx,py - qy) + max(qx,qy))
scale factor = max(qx,qy)
Here, px,qx and py,qy are the converted precisions and scale factors of x and y, respectively.
The MAX built-in function is also a preprocessor built-in function; however, the preprocessor does not permit scale factors.
Dialects - ansi, dec
The MAXLENGTH built-in function returns a fixed binary number representing the maximum possible length of a varying-length character string. The format is:
MAXLENGTH (string)stringA reference to a character string or a bit string. If it is anything other than a varying-length character string, the MAXLENGTH function returns a result identical to the result that would be returned by the LENGTH built-in function.
For example:
MAXLENGTH_EXAMPLE: PROCEDURE OPTIONS(MAIN); DCL CHAR_VAR CHARACTER(10) VARYING; CHAR_VAR = 'String'; CALL SAMPLE(CHAR_VAR); END MAXLENGTH_EXAMPLE; SAMPLE: PROCEDURE(STRING); DCL STRING CHAR(*) VARYING; PUT LIST(LENGTH(STRING),MAXLENGTH(STRING)); END SAMPLE;
The program prints the following:
6 10
11.4.56. MIN
Dialects - ansi, dec, ibm
The MIN built-in function returns the smaller of two arithmetic expressions x and y. The format is:
MIN(x,y)
Returned Value
The expressions x and y are converted to their derived type before the operation is performed (for a discussion of derived types see Section 6.4.2, “Derived Data Types for Arithmetic Operations”. If the derived type is floating point, the value returned is also floating point, with the larger precision of the two converted arguments. If the derived type is fixed point, the returned value is a fixed-point value with the base of derived type. The value has the following attributes:
precision = min(31,max(px - qx,py - qy) + max(qx,qy))
scale factor = max(qx,qy)
Here, px,qx and py,qy are the converted precisions and scale factors of x and y.
The MIN built-in function is also a preprocessor built-in function; however, the preprocessor does not permit scale factors.
11.4.57. MOD
Dialects - ansi, dec, ibm
The MOD built-in function returns, for an arithmetic expression x and nonnegative arithmetic expression y, the value r that equals x modulo y. That is, r is the smallest positive value that must be subtracted from x to make the remainder of x divided by y exactly 0.
The format of the function is:
MOD(x,y)
Returned Value
The expressions x and y are converted to their derived type before the operation is performed.
If the derived type is unscaled fixed point, then the precision of the result is the precision of the second operand.
If the derived type is floating point, the returned value is an approximation in floating point, with the larger of the precisions of the two converted arguments.
The value returned is:
u - w * floor(u / w)
The arguments u and w become the arguments x and y, respectively, after conversion to their derived type. If w is zero, u is converted to the precision described below, which can signal FIXEDOVERFLOW.
If x and y are fixed-point expressions, a fixed-point value is returned. The value has the following attributes:
precision = min(31,pw - qw + max(qu,qw))
scale factor = max(qu,qw)
Here, qu is the scale factor of u, pw is the precision of w, and qw is the scale factor of w. The FIXEDOVERFLOW condition is signaled if the following is true:
pw - qw + max(qu,qw) > 31
The MOD built-in function is also a preprocessor built-in function; however, the preprocessor does not permit scale factors.
Examples
MODEX: PROCEDURE OPTIONS(MAIN); DECLARE OUTMOD PRINT FILE; ON FIXEDOVERFLOW PUT FILE(OUTMOD) SKIP LIST('FIXEDOVERFLOW signaled'); PUT FILE(OUTMOD) SKIP LIST(MOD(28,128)); PUT FILE(OUTMOD) SKIP LIST(MOD(130,128)); PUT FILE(OUTMOD) SKIP LIST(MOD(-28,128)); PUT FILE(OUTMOD) SKIP LIST(MOD(4.5,.758)); PUT FILE(OUTMOD) SKIP LIST(MOD(-4.5,.758)); PUT FILE(OUTMOD) SKIP LIST(MOD(1.5E-3,-1.4E-3)); PUT FILE(OUTMOD) SKIP LIST(MOD(28,0)); END MODEX;
The program MODEX writes the following output to OUTMOD.DAT:
28
2
100
0.710
0.048
-1.3E-03
FIXEDOVERFLOW signaledThe last PUT statement attempts to take MOD(28,0). The constants 28 and 0 are both fixed-point decimal expressions, with precisions (2,0) and (1,0), respectively. Therefore, the attributes of the returned value are determined to be FIXED DECIMAL. The value has the following attributes:
precision = min(31,1 - 0 + max(0,0)) = 1
scale factor = max(0,0) = 0
Although 28 modulo 0 is 28, MOD(28,0) signals FIXEDOVERFLOW because 28 cannot be represented in the result precision. (The value of the function is therefore undefined.)
Dialects - ansi, dec, ibm
The MULTIPLY built-in function multiplies two arithmetic expressions x and y, and returns the product of the two values with a specified precision p and an optionally specified scale factor q.
The format of the function is:
MULTIPLY(x,y,p[,q])
pAn integer constant greater than zero and less than or equal to the maximum precision of the result type, which is 31 for fixed-point data, 15 for floating-point decimal data, and 53 for floating-point binary data.
qAn integer in the range -31 through p, when used with fixed-point binary multiplication. The scale factor for fixed-point decimal multiplication has a range 0 through p. A scale factor is not to be used with floating-point arithmetic. If no scale factor is designated, q defaults to zero.
Expressions x and y are converted to their derived type before the multiplication is performed.
For example:
MULT: PROCEDURE OPTIONS (MAIN); DECLARE I_RATE FIXED DECIMAL(31,4), PRINCIPAL FIXED DECIMAL(31,2), OWED FIXED DECIMAL(31,6); I_RATE = .1514; PRINCIPAL = 27688.25; OWED = MULTIPLY (I_RATE,PRINCIPAL,31,6); PUT SKIP LIST ('INTEREST OWED =',OWED); END;
Interest rates are calculated to six decimal places and the following string is printed:
INTEREST OWED = 4192.001050
11.4.59. NULL
Dialects - ansi, dec, ibm
The NULL built-in function returns a null pointer value. The format is:
NULL()
Examples
IF NEXT_POINTER = NULL() THEN CALL FINISH;
The IF statement checks whether the pointer variable NEXT_POINTER is null; if so, the CALL statement is executed.
The NULL built-in function can be used for offset variables as well as pointer variables, because the compiler automatically performs conversions between pointer and offset values.
11.4.60. OFFSET
Dialects - ansi, dec, ibm
The OFFSET built-in function converts a pointer to an offset relative to a designated area. If the pointer is null, the result is null. The format of the function is:
OFFSET(pointer,area)
pointerA reference to a pointer variable whose current value either represents the location of a based variable within the specified area or is null.
areaA reference to a variable declared with the AREA attribute. If the specified pointer is not null, it must designate a storage location within this area.
Examples
DECLARE MAP_SPACE AREA (2048), START OFFSET (MAP_SPACE), QUEUE_HEAD POINTER; ⋮ START = OFFSET (QUEUE_HEAD,MAP_SPACE);
The offset variable START is associated with the area MAP_SPACE. The OFFSET built-in function converts the value of the pointer to an offset value.
Dialects - ansi, dec, ibm
The ONCHAR built-in function returns the character that caused a CONVERSION condition to be raised. If there is no active CONVERSION condition, the return value is a single space.
The format of the function is:
ONCHAR()
The ONCHAR value is actually a single character substring of the ONSOURCE built-in function value, unless there is no active CONVERSION condition.
11.4.62. ONCODE
Dialects - ansi, dec, ibm
The ONCODE built-in function returns a fixed-point binary integer that is the status value of the most recent run-time error that signaled the current ON condition. You can use the function in any ON-unit to determine the specific error that caused the condition. If the function is used within any context outside an ON-unit, it returns a zero. The format is:
ONCODE()
11.4.63. ONFILE
Dialects - ansi, dec, ibm
The ONFILE built-in function returns the name of the file constant for which the current file-related condition was signaled. The format is:
ONFILE()
This built-in function can be used in an ON-unit established for any of the following conditions:
An ON-unit for the KEY, ENDFILE, ENDPAGE, and UNDEFINEDFILE conditions
A VAXCONDITION ON-unit established for I/O errors that can occur during file processing
An ERROR ON-unit that receives control as a result of the default PL/I action for file- related errors, which is to signal the ERROR condition
A CONVERSION ON-unit that was entered because of an error that occurred during conversion of data in a GET statement
Returned Value
The returned value is a varying-length character string. The ONFILE function returns a null string if referenced outside an ON-unit, within an ON-unit that is executed as a result of a SIGNAL statement, or within a CONVERSION ON-unit that was not entered because of a conversion in a GET statement.
11.4.64. ONKEY
Dialects - ansi, dec, ibm
The ONKEY built-in function returns the key value that caused the KEY condition to be signaled during an I/O operation to a file that is being accessed by key. Its format is:
ONKEY()
This built-in function can be used in an ON-unit established for these conditions:
KEY, ENDFILE, or UNDEFINEDFILE
An ERROR ON-unit that receives control as a result of the default PL/I action for the KEY condition, which is to signal the ERROR condition
Returned Value
The returned key value is a varying-length character string. The ONKEY built-in function returns a null string if referenced outside an ON-unit or within an ON-unit executed as a result of the SIGNAL statement.
11.4.65. ONLOC
Dialects - ibm
The ONLOC built-in function returns the hexadecimal address of the entry point used for the current invocation of the procedure in which a condition was raised. Its format is:
ONLOC
This built-in function can be used in any ON-unit or any of the dynamic descendants of an ON-unit.
Returned Value
The returned key value is a varying-length character string. The ONKEY built-in function returns a null string if referenced outside an ON-unit or within an ON-unit executed as a result of the SIGNAL statement.
Dialects - ansi, dec, ibm
The ONSOURCE built-in function returns the source string that was being converted when the CONVERSION condition was raised. If no CONVERSION condition is active, the return value is a null string.
The format of the function is:
ONSOURCE()
11.4.67. PLIRETV
Dialects - ibm
The PLIRETV built-in function returns the PL/I return code, a fixed binary (15) value. The PL/I return code is set by the PLIRETC built-in subroutine: it is zero when the program starts, and keeps the value of the most recent call to PLIRETC, in any procedure. The Sort program also sets it, to 4 before passing the first record to an output routine.
PLIRETC and PLIRETV are used chiefly with the Sort program, by the input and output routines a program passes to PLISRTB, PLISRTC and PLISRTD. A program can also use them for its own purposes, to pass a value from one procedure to another. The PL/I return code is not the program's exit status: it is not passed to the operating system when the program ends. See the Endless PL/I for POSIX Systems User's Manual for the Sort program and PLIRETC.
The format of the function is:
PLIRETV
For example, the following statements set the PL/I return code and test it:
CALL PLIRETC(8); IF PLIRETV = 8 THEN PUT SKIP LIST ('No more records.');
Dialects - ansi, dec, ibm
The POINTER built-in function returns a pointer to the location identified by the referenced offset and area. The format is:
{ POINTER } (offset,area)
{ PTR }offsetA reference to an offset variable whose current value either represents the offset of a based variable within the specified area or is null.
areaA reference to a variable that is declared with the AREA attribute and with which the specified offset value is associated.
Returned Value
The returned value is of type POINTER. If the offset value is null, the result is null.
Examples
DECLARE MAP_SPACE AREA (2048), START OFFSET (MAP_SPACE), P POINTER; ⋮ P = POINTER (START,MAP_SPACE);
The POINTER built-in function converts the value of the offset variable START (in the area MAP_SPACE) to a pointer value.
Dialects - ansi, dec
The POSINT built-in function treats specified storage as an unsigned integer, and returns the value of the integer. The format is:
POSINT(expression[,position[,length]])
expressionA scalar expression or reference to connected storage. This reference must not be an array, structure, or named constant. If position and length are not specified, the length of the referenced storage must not exceed 32 bits. (If it exceeds 32 bits, a FATAL run-time error results.)
positionA positive integer value that denotes the position of the first bit in the field. If omitted, position defaults to 1, signifying the first bit of the storage denoted by the expression. If specified, position must satisfy the following condition:
1 <= position <= size(expression)
Size(expression) is the length in bits of the storage denoted by expression. A position equal to size(expression) implies a zero-length field.
lengthAn integer value in the range 0 through 32 that specifies the length of the field. If omitted, length is the number of bits from the bit denoted by position through the end of the storage denoted by expression. If specified, length must satisfy the following condition:
0 <= length <= size(expression) - position
Size(expression) is the length in bits of the storage denoted by expression.
Returned Value
The value returned by POSINT is of the type FIXED BINARY (31). If the field has a length of zero, POSINT returns zero.
Because the POSINT built-in function treats storage as if it contained an unsigned integer, the value returned can be larger than the maximum positive value that can be contained in the signed integer that is stored in the same number of bits. For example, if the argument to POSINT is 32 bits long and has the high-order (sign) bit set, then the resulting value is too large for assignment to a FIXED BIN (31) variable. The result of such an operation is undefined.
Examples
The use of the POSINT built-in function is identical to the use of the INT built-in function, except that POSINT treats its argument as an unsigned integer. The following example illustrates this difference.
DECLARE (X15,Y15,I15,P15) FIXED BIN (15), P31 FIXED BIN (31); X15 = 585; Y15 = -585; I15 = INT(X15); /* I15 = 585 */ I15 = INT(Y15); /* I15 = -585 */ P15 = POSINT(X15); /* P15 = 585 */ P31 = POSINT(Y15); /* P31 = 64951 */ P15 = POSINT(Y15); /* ERROR signaled */
In this example, POSINT first assigns the storage referenced by X15 to P15. Because this storage is occupied by a positive integer and therefore has the sign bit clear, POSINT behaves exactly like INT. However, when POSINT is applied to storage occupied by a negative integer, it interprets the set sign bit as representing part of the integer. When the resulting value is assigned to a FIXED BIN (31) variable, it is seen to be larger than the FIXED BIN (15) variable results in PL/I signaling an ERROR condition.
Dialects - ibm
The PRECISION built-in function takes an input expression and returns the value converted to the specified precision. The format of the PRECISION built-in function is:
PRECISION(expression,digits[,scale]);
expression
Any valid arithmetic expression.
digits
An integer specifying the number of digits of precision to which the expression will be converted. It must not exceed the dialect-defined limit for the data type of the expression.
scale
An optional integer constant less than or equal to the specified precision. The scale factor can be optionally signed. It must not exceed the dialect-defined limit for the data type of the expression. The default scale factor is zero.
Do not use a scale factor with floating point expressions.
Dialects - ansi, dec, ibm
The PROD built-in function takes an array as an argument and returns the arithmetic product of all the elements in the array. The array must have the FIXED or the FLOAT attribute. The format of the PROD built-in function is:
PROD(array-variable);If the array has the attributes FIXED(p,0), the result will have the attributes FIXED(p,0). If the array has the attributes FLOAT(p), the result will also have the attributes FLOAT(p). If the array has the attributes FIXED(p,q) with q not equal to 0, the result will have the attributes FLOAT(p).
The result will have the same base attribute as the array, either DECIMAL or BINARY.
Note that the PROD built-in function does not perform matrix multiplication of two arrays.
11.4.72. RANK
Dialects - ansi, dec
The RANK built-in function returns a fixed-point binary integer that is the ASCII code for the designated character. The precision of the returned value is 15. The format of the function is:
RANK(character)characterAny expression yielding a 1-character value.
Examples
CODE = RANK('A'); /* CODE = 65 */ CODE = RANK('a'); /* CODE = 97 */ CODE = RANK('$'); /* CODE = 36 */
The ASCII characters are the first 128 characters of the DEC Multinational Character Set.
11.4.73. REPEAT
Dialects - ibm
The REPEAT built-in function copies a given string a specified number of times and concatenates the result into a single string. Its format is:
REPEAT(string,count)
stringAny bit- or character-string expression. If the expression is a bit string, the result is a bit string. Otherwise, the result is a character string.
countAny expression that yields an integer. The specified count controls the number of repeats of the string that are concatenated to the original string, as follows:
Value of Count String Returned 0 or negative The string argument 1 to n COUNT+1 concatenated copies of the string argument
Examples
The REPEAT built-in function is similar to the COPY built-in function, but not exactly the same. Compare the value returned by this example to the one returned by the example for the COPY built-in function:
REPEAT('12',3)
This function reference returns the character-string value '12121212'.
Dialects - ansi, dec
The REVERSE built-in function reverses the characters or bits in a string. It takes one argument, which is either a character string (fixed or varying) or a bit string. It returns a string of the same type and size as its argument, with all the characters (bytes) or bits reversed.
The format of the function is:
REVERSE(string-expression);string-expressionAn expression that evaluates to a character string or a bit string.
Examples
DECLARE X CHARACTER(4) VARYING, Y BIT(8); X = REVERSE('abc') Y = REVERSE('00010101'B)
The character-string variable X is assigned the value `cba'. The bit-string variable Y is assigned the value '10101000'B.
11.4.75. ROUND
Dialects - ansi, dec, ibm
The ROUND built-in function rounds a fixed-point binary decimal expression to a specified number of binary or decimal places respectively. The format is:
ROUND(expression,position)
expressionAn arithmetic expression that yields a fixed-point binary or decimal value; or a pictured value with fractional digits. A binary value can have a positive or negative non-zero scale factor, but a decimal value must have a positive non- zero scale factor.
positionA nonnegative integer constant specifying the number of binary or decimal places in the rounded result.
Returned Value
Where the arguments are an expression of type FIXED BINARY(p,q) or type FIXED DECIMAL(p,q) and position k, the returned value is the rounded value with the following attributes:
precision = max(1,min(p - q + k + 1,31))
scale factor = k
For a fixed binary number, the rounded value is:
ROUND(x,k) = sign(x)*(2 -k+1 )*floor(abs(x)*(2 k ) + 1)
For a fixed decimal number, the rounded value is:
ROUND(x,k) = sign(x)*(10 -k )*floor(abs(x)*10 k ) + 0.5)
Example 1
The following sample program shows rounding of scaled fixed binary numbers:
r: procedure options(main); declare fb1 fixed binary(31, 8), fb2 fixed binary(31, 4), fb3 fixed binary(31, 2), fb4 fixed binary(31, 2), fb5 fixed binary(31, 2); fb1 = 16.8750; /* 7/8 */ fb2 = 15.4375; /* 7/16 */ fb3 = 128.25; /* 128 1/4 */ fb4 = 128.75; /* 128 3/4 */ fb5 = -128.75; /* -128 3/4 */ put skip edit ('round(16.8750, 2)=',round(fb1,2)) (a,col(20),f(15,5)); put skip edit ('round(16.8750, 3)=',round(fb1,3)) (a,col(20),f(15,5)); put skip edit ('round(15.4375, 1)=',round(fb2,1)) (a,col(20),f(15,5)); put skip edit ('round(128.25,0)=',round(fb3,0)) (a,col(20),f(15,5)); put skip edit ('round(128.75,0)=',round(fb4,0)) (a,col(20),f(15,5)); put skip edit ('round(-128.75,0)=',round(fb5,0)) (a,col(20),f(15,5)); end r;
This program produces the following output. Note that 16.87500 equals 16 7/8 in fractional notation and that 15.50000 equals 15 1/2 in fractional notation.
round(16.8750, 2)= 17.00000 round(16.8750, 3)= 16.87500 round(15.4375, 1)= 15.50000 round(128.25,0)= 128.00000 round(128.75,0)= 129.00000 round(-128.75,0)= -129.00000
Example 2
The following example shows rounding of scaled fixed decimal numbers:
A = 1234.567; Y = ROUND(A,1); /* Y = 1234.6 */ Y = ROUND(A,0); /* Y = 1235 */ A = -1234.567; Y = ROUND(A,2); /* Y = -1234.57 */
Dialects - ansi, dec
The SEARCH built-in function takes two character-string arguments and attempts to locate the first character in the first string that is also present in the second string. The search for a match is carried out from left to right. If one character is matched, the function returns the position of that character in the first string. This function is case sensitive.
The format is:
SEARCH(string-1,string-2[,starting-position])
string-1A character-string expression. One character in the string is to be matched, if possible, in the second string.
string-2A character-string expression to be compared, character by character, with each character in the first string, in order, until one matching character is found.
starting-positionA positive integer in the range 1 to n+1, where n is the length of the first string. It specifies the leftmost character in the first string from which the search is to begin. If
starting-positionis specified, any characters to the left of that position in the first string are ignored. (By default, the search begins with the leftmost character in the first string.)
Returned Value
The returned value is a positive integer representing the position in string-1 of the first
character that is also found in string-2. If no match is found, the returned value is
zero.
Examples
DECLARE STR1 CHARACTER(20) VARYING, STR2 CHARACTER(10) INITIAL ('ABCDEFGHIJ'), X FIXED DECIMAL(2); STR1 = 'BARBARA'; X = SEARCH (STR1,STR2);
In this example, X is given the value 1 because the first character (`B') in STR1 (`BARBARA') is found in STR2 (`ABCDEFGHIJ').
STR1 = '12-GEORGE'; X = SEARCH (STR1,STR2);
Here, X is given the value 4. `G' is in the fourth position in `12-GEORGE' and is the first character in STR1 that is also present in STR2 (`ABCDEFGHIJ').
X = SEARCH (STR1,STR2,6);
X is given the value 8. The starting-position parameter, 6, causes the search to begin with
the sixth character in `12-GEORGE', and thus the first matching character is the second `G', which is in the eighth
position.
PUT LIST (SEARCH('ZZZBAD','ABCD'));
The function returns the value 4 because the position of `B' in `ZZZBAD' is 4, and `B' is the leftmost matching character. Here, constants are used instead of variables.
PUT LIST (SEARCH('ABCD','ZZZBAD'));
This statement is the same as the preceding one except that the parameters are reversed. Now the value returned is 1 instead of 4 because `A', the first character in `ABCD', is matched. Note that the order in which the parameters are given is crucial. Note also that duplicate characters in the second string never change the result.
PUT LIST (SEARCH (' TEST 123','0123456789'));
The function returns the value 9 because `1', which is in the ninth position, is the first character matched in the second string.
Dialects - ansi, dec, ibm
The SIGN built-in function returns 1, -1, or 0, indicating whether an arithmetic expression is positive, negative, or zero, respectively. The returned value is a fixed-point binary integer.
The format of the function is:
SIGN(expression)11.4.78. SIN
11.4.79. SIND
11.4.80. SINH
Dialects - ansi, dec
The SIZE built-in function returns a fixed-point binary integer that is the number of bytes allocated to a referenced variable. The format is:
SIZE (reference)referenceThe name of a variable known to this block. The variable can be a scalar variable, an array or structure, or a structure member. The variable cannot be a constant or expression. Although references to individual array elements are allowed, the returned value in this instance is the size of the entire array, not the element.
Returned Value
The returned value is a fixed bin(31) value containing the variable's allocated size in bytes. For bit strings that do not exactly fill an integral number of bytes, the value is rounded up to the next byte.
For varying character-string variables, note that the returned value is two bytes greater than the declared length of the string. These extra two bytes are allocated by PL/I to contain the current length of the string. (If you want the value of the maximum length of a varying character string, use the MAXLENGTH built-in function. If you want the value of the current length of a varying character string, use the LENGTH built-in function.)
Examples
The following example illustrates the use of the SIZE built-in function on some scalar variables.
DECLARE S FIXED BINARY(31), INT FIXED BINARY(15), CHAR1 CHARACTER(5), CHAR2 CHARACTER(5) VARYING, BITSTRING BIT(10), P POINTER; S = SIZE(INT); /* S = 2 */ S = SIZE(CHAR1); /* S = 5 */ S = SIZE(CHAR2); /* S = 7 */ S = SIZE(BITSTRING); /* S = 2 */ S = SIZE(P); /* S = 4 */
Note the difference between the allocated size for the fixed-length and varying character strings. Note also that the returned value for the bit string is rounded up to 2 bytes, the integral number of bytes required to contain 10 bits.
DECLARE 1 STRUC, 2 CHARSTR CHARACTER(5), 2 BITSTR BIT(10), ARRAY(5) FIXED BINARY(31), S FIXED BINARY(31); S = SIZE(STRUC); /* S = 7 */ S = SIZE(CHRSTR); /* S = 5 */ S = SIZE(ARRAY); /* S = 20 */ S = SIZE(ARRAY(2)); /* S = 20 */
In this example, the SIZE built-in function is applied to a structure, to one of its members, to an array, and to an element of the array. Note that a reference to an array element returns the same value as a reference to the entire array.
DECLARE 1 TARGET, 2 A BIT(9), 2 B BIT(10), 2 C BIT(1), 1 ALIGNED_TARGET, 2 A BIT(9) ALIGNED, 2 B BIT(10) ALIGNED, 2 C BIT(1) ALIGNED, S FIXED BINARY(31); S = SIZE(TARGET); /* S = 3 */ S = SIZE(ALIGNED_TARGET); /* S = 5 */
This example illustrates the difference in PL/I's storage of unaligned and aligned bit strings. The structure TARGET consists of three bit strings that are unaligned (the default storage mechanism). The three bit strings occupy 20 consecutive bits in memory. Therefore, only three bytes are required to hold the structure. The structure ALIGNED_TARGET consists of the same three strings, except each is declared with the ALIGNED attribute, forcing the structure to start on a byte boundary. In this structure, A and B each require two bytes while C requires one byte, for a total of five bytes. A similar situation exists with arrays of bit strings.
T: PROC OPTIONS(MAIN); DCL P PTR; DCL 1 S BASED(P), 2 I FIXED, 2 A(10 REFER(I)) FIXED; ALLOCATE S; PUT SKIP LIST(SIZE(S)); /* Returns 44 */ I = 5; PUT SKIP LIST(SIZE(S)); /* Returns 24 */ END;
This example shows how the SIZE built-in function works on a structure containing the REFER option. SIZE returns the current size.
DECLARE STR CHARACTER(10) VARYING; ⋮ CALL SUB(STR); ⋮ SUB: PROCEDURE(X); DECLARE X CHARACTER(*) VARYING; PUT SKIP LIST (SIZE(X));
Here, the SIZE built-in function is used to determine the size of a parameter that is passed to a procedure. This PUT statement prints the value 12.
CALL MACRO_ROUTINE( ADDR(OUTSTRING),SIZE(OUTSTRING) );
Here, the SIZE built-in function is used to supply an argument to a procedure (possibly one written in another language) that requires the size in bytes of a data structure.
Dialects - ansi, dec
The SOME built-in function allows you to determine whether at least one bit in a bit string is '1'B. In other words, it performs a logical OR operation on the elements of the bit string. The format of the SOME built-in function is:
SOME(bit-string)The function returns the value '1'B if one or more bits in the bit-string argument are '1'B. It returns '0'B if every bit in the argument is '0'B or if the argument is the null bit string.
11.4.83. SQRT
Dialects - ansi, dec, ibm
The SQRT built-in function returns a floating-point value that is the square root of an arithmetic expression x. The square root is computed in floating point. After its conversion to floating point, x must be greater than or equal to zero.
The format of the function is:
SQRT(x)11.4.84. STORAGE
Dialects - ibm
The STORAGE built-in function returns a fixed binary (31) value that is the implementation-defined storage, in bytes, required by the specified variable. The format of the function is:
STORAGE(reference)referenceThe name of a variable known to this block except:
A variable that is not in connected storage.
An unaligned fixed-length BASED, DEFINED, subscripted, parameter, or structure element.
A minor structure with a first or last element that is an unaligned fixed-length bit string, unless that element is the first or last element of the containing major structure.
A major structure with the BASED, DEFINED, or parameter attribute with a first or last element that is an unaligned fixed-length bit string.
Returned Value
The returned value is a fixed bin(31) value containing the variable's maximum storage size in bytes. This is the maximum number of bytes that could be transmitted if you wrote the variable to a record file defined with the ENVIRONMENT(SCALARVARYING) option.
Some variables can have varying sizes. If the referenced variable is a varying-length string, a scalar area, or an aggregate that contains areas or varying-length strings, the returned value includes the storage used by any control bytes and length prefixes plus the maximum lengths of the strings and the maximum sizes of the areas.
11.4.85. STRING
Dialects - ansi, dec, ibm
The STRING built-in function concatenates the elements of an array or structure and returns the result. Elements of a string array are concatenated in row-major order. Members of a structure are concatenated in the order in which they were declared.
The format of the STRING built-in function is:
STRING(reference)referenceA reference to a variable that is suitable for bit-string or character-string overlay defining. Briefly, a variable is suitable if it consists entirely of characters or bits, and these characters or bits are packed into adjacent storage locations, without gaps.
Returned Value
The string returned is of type CHARACTER or BIT, depending on whether the reference is suitable for character- or bit-string overlay defining. The length of the string is the total number of characters or bits in the base reference.
Examples
STRING_BIF_EXAMPLE: PROCEDURE; DECLARE NEW_NAME CHARACTER(40); DECLARE 1 FULL_NAME, 2 FIRST_NAME CHARACTER(10), 2 MIDDLE_INITIAL CHARACTER(3), 2 LAST_NAME CHARACTER(27); FIRST_NAME = 'MABEL'; MIDDLE_INITIAL = 'S.'; LAST_NAME = 'MERCER'; NEW_NAME = STRING(FULL_NAME); /* NEW_NAME = 'MABEL S. MERCER ' where is a space */ END STRING_BIF_EXAMPLE;
11.4.86. SUBSTR
Dialects - ansi, dec, ibm
The SUBSTR built-in function returns a specified substring from a string. The format is:
SUBSTR(string,position[,length])
stringA bit- or character-string expression.
positionAn integer expression that indicates the position of the first bit or character in the substring. The position must be greater than or equal to 1 and less than or equal to LENGTH(string) + 1.
lengthAn integer expression that indicates the length of the substring to be extracted. If not specified, length is:
LENGTH(string) - position + 1
In other words, if length is not specified, the substring is extracted beginning at the indicated position and ending at the end of the string.
The length must satisfy the following condition:
0 <= length <= LENGTH(string) - position + 1
Returned Value
The returned substring is of type BIT(length) or CHARACTER(length), depending on the type of the string argument. If the length argument is zero, the result is a null string.
Examples
DECLARE (NAME,LAST_NAME) CHARACTER(20), START FIXED BINARY(31); NAME = 'ISAK DINESEN'; /* NAME = 'ISAK#DINESEN ' */ START = INDEX(NAME,' ') + 1; /* START = 6 */ LAST_NAME = SUBSTR(NAME,START); /* default length = LENGTH(NAME) - START + 1 = 15 */ /* LAST_NAME = 'DINESEN ' */
Dialects - ansi, dec
The SUBTRACT built-in function returns the difference of two arithmetic expressions x and y, with a specified precision p and an optionally specified scale factor q. The format of the function is:
SUBTRACT(x,y,p[,q])
pAn unsigned integer constant greater than zero and less than or equal to the maximum precision of the result type, which is 31 for fixed-point data, 15 for floating-point decimal data, and 53 for floating-point binary data.
qAn integer constant less than or equal to the specified precision. The scale factor can be optionally signed when used in fixed-point binary subtraction. The scale factor for fixed-point binary must be in the range -31 through p. The scale factor for fixed-point decimal data must be in the range 0 through p. If you omit q, the default value is zero. Do not use a scale factor for floating-point arithmetic.
Expressions x and y are converted to their derived type before the subtraction is performed. For example:
SUBTRACTBIF: PROCEDURE OPTIONS (MAIN); DECLARE X FIXED DECIMAL (8,3), Y FIXED DECIMAL (8,3), Z FIXED DECIMAL (9,3); X=9500.374; Y=2278.897; Z = SUBTRACT (X,Y,9,3); PUT SKIP LIST ('DIFFERENCE =',Z); END;
This program prints:
DIFFERENCE = 7221.477
Dialects - ansi, dec, ibm
The SUM built-in function takes an array as an argument and returns the arithmetic sum of all the elements in the array. The array must have the FIXED or the FLOAT attribute. The format of the SUM built-in function is:
SUM(array-variable)If the array has the attributes FIXED(p,q), the result will have the attributes FIXED(p,q). If the array has the attributes FLOAT(p), the result will also have the attributes FLOAT(p).
The result will have the same base attribute as the array, either DECIMAL or BINARY.
11.4.89. TAN
Dialects - ansi, dec, ibm
The TAN built-in function returns a floating-point value that is the tangent of an arithmetic expression x, where x represents an angle in radians. The tangent is computed in floating point. After its conversion to floating point, x must not be an odd multiple of /2
The format of the function is:
TAN(x)11.4.90. TAND
Dialects - ansi, dec, ibm
The TAND built-in function returns a floating-point value that is the tangent of an arithmetic expression x, where x represents an angle in degrees. The tangent is computed in floating point. After its conversion to floating point, x must not be an odd multiple of 90.
The format of the function is:
TAND(x)11.4.91. TANH
Dialects - ansi, dec, ibm
The TIME built-in function returns an 8-character string representing the current time of day in the following form:
hhmmssxxThe current hour (00-23)
The minutes (00-59)
The seconds (00-59)
Hundredths of seconds (00-99)
The format of the TIME built-in function is:
TIME()
Returned Value
If TIME is used as a preprocessor built-in function, the time returned is the time when the program was compiled; otherwise the function returns the time at run time.
Dialects - ansi, dec, ibm
Given a character-string argument, the TRANSLATE built-in function replaces occurrences of an old character with a corresponding translation character and returns the resulting string. The format is:
TRANSLATE(original,translation[,old-chars])
originalA character-string expression in which specific characters are to be translated.
translationA character-string expression giving replacement characters for corresponding characters in
old-chars.old-charsA character-string expression indicating which characters in the original are to be replaced. If
old-charsis not specified, the default is COLLATE().If the translation is shorter than
old-chars, the translation is padded on the right with spaces to the length ofold-charsbefore any translation occurs. If the translation is longer thanold-chars, its excess characters (on the right) are ignored.The following steps are performed for each character (beginning at the left) in the original:
Let original(i) be the current character in the original string, and let result(i) be the corresponding character in the resulting string.
Search
old-charsfor the leftmost occurrence of original(i).If
old-charsdoes not contain original(i), then let result(i) equal original(i). Otherwise, let j equal the position of the leftmost occurrence of original(i) inold-chars, and let result(i) equal translation(j).Return to step 1.
Returned Value
The string returned is of type CHARACTER(length), where length is the length of the original string. If the original string is a null string, the returned value is a null string.
Examples
TRANSLATE_XM: PROCEDURE OPTIONS(MAIN); DECLARE NEWSTRING CHARACTER(80) VARYING; DECLARE TRANSLATION CHARACTER(128); DECLARE I FIXED; DECLARE COLLATE BUILTIN; /* translate space to '0': */ NEWSTRING = TRANSLATE('1 2','0',' '); PUT SKIP LIST(NEWSTRING); /* translate letter 'F' to 'E': */ NEWSTRING = TRANSLATE('BFFLZFBUB','E','F'); PUT SKIP LIST(NEWSTRING); /* change case of letters in sentence */ TRANSLATION = COLLATE; DO I=RANK('A') TO RANK('Z'); /* replace upper with lower */ SUBSTR(TRANSLATION,I,1) = SUBSTR(COLLATE,I+32,1); END; DO I=RANK('a') TO RANK('z'); /* replace lower with upper */ SUBSTR(TRANSLATION,I,1) = SUBSTR(COLLATE,I-32,1); END; NEWSTRING = TRANSLATE('THE QUICK BROWN fox JUMPS OVER THE LAZY dog',TRANSLATION); PUT SKIP LIST(NEWSTRING); END TRANSLATE_XM;
The first reference translates the string `1 2' to `102' The second reference translates `BFFLZFBUB' to `BEELZEBUB'. The third reference produces the following new sentence:
'the quick brown FOX jumps over the lazy DOG'
Dialects - ansi, dec
The TRIM built-in function accepts a character string as an argument and returns a character string that consists of the input string with specified characters removed from the left and right. If you supply only one argument, TRIM removes blanks from the left and right of the argument. If you supply only the second argument, TRIM removes the specified characters from the left of the string and blanks from the right. If you supply both the second and third arguments, TRIM removes characters specified by those arguments from the left and right of the string, respectively.
The format of the TRIM built-in function is:
TRIM (input-string,[beginning-chars,end-chars])
input-stringA character-string variable or constant. This argument supplies the string from which characters are to be trimmed.
beginning-charsA character-string variable or constant. This argument specifies characters to be trimmed from the left of the input string. If a character that is in the first position in the input string is also present anywhere in
beginning-chars, that character is removed from the input string. This process is repeated until a character is encountered on the left of the input string that is not present inbeginning-chars, or until the characters in the input string are exhausted.end-charsA character-string variable or constant. This argument specifies characters to be trimmed from the right of the input string. The process of removing characters from the right is identical to that of removing characters from the left, except that the character in the last position is examined.
The TRIM built-in function accepts either one or three arguments. Any of the arguments can consist of a null string; specifically, if
beginning-charsorend-charsis null, no characters are removed from the corresponding end of the input string.When only one argument is supplied, TRIM removes blanks from both ends of that argument. In other words, the following two expressions are equivalent:
TRIM(S) TRIM(S,' ',' ')
Returned Value
The returned value is a character string with characters removed from the ends.
Examples
The following examples illustrate the use of the TRIM built-in function.
| Text | Returned String |
| TRIM ('ABC') | 'ABC' |
| TRIM(' ABC') | 'ABC' |
| TRIM(' ABC ') | 'ABC' |
| TRIM('ABC ') | 'ABC' |
| TRIM(' ABCDEF','','E') | ' ABCDEF' |
| TRIM(' ABCDEF','','FE') | ' ABCD' |
| TRIM('ABCDEF','CADB','FE') | '' |
| TRIM(' ABCDEF ','ABC ',' EDF') | '' |
| TRIM('AAAABCCXCCDDDDEFFFF','AC','DF') | 'BCCXCCDDDDE' |
11.4.95. TRUNC
Dialects - ansi, dec, ibm
The TRUNC built-in function changes all fractional digits in an arithmetic expression x to zeros and returns the resulting integer value. Its format is:
TRUNC(x)Returned Value
If x is a floating-point expression, the returned value is a floating-point value. If x is a fixed-point expression, the returned value is a fixed-point value with the same base as x. The value has the following attributes:
precision = min(31,p - q + 1)
scale factor = 0
Here, p and q are the precision and scale factor of x.
11.4.96. UNSPEC
Dialects - ansi, dec, ibm
The UNSPEC built-in function returns a bit string representing the internal coded value of the referenced variable, or a specified part of that variable. The variable can be an aggregate or a scalar variable of any type. The format of the function is:
UNSPEC(reference)Returned Value
The returned value is a bit string whose length is the number of bits occupied by the referenced variable. The length of the bit string must be less than or equal to the maximum length for bit-string data. The returned bit string contains the contents of the storage of the referenced variable , the first bit in storage being the first bit in the returned value. The actual value may differ depending on the architecture of your platform. For example, the VAX and Alpha architecture store integers differently than the IBM architecture. Therefore, use the UNSPEC built-in function with caution when developing cross-platform software.
Note that if the referenced variable is a binary integer (FIXED BINARY), the first bit in the returned value is the lowest binary digit.
Examples
DECLARE X CHARACTER(2), Y BIT(16); X = 'AB'; Y = UNSPEC(X); ⋮ DECLARE I FIXED BINARY(15); I = 2; PUT LIST(UNSPEC(I));
As a result of the first UNSPEC reference, Y contains the ASCII codes of 'A' and 'B'. The PUT LIST statement containing UNSPEC(I) prints the following string:
'0100000000000000'BDialects - ansi, dec
The VARIANT preprocessor built-in function returns a string representing the value of the variant qualifier in the command that invoked the compilation.
The format in a preprocessor expression is:
VARIANT()
The -T option permits specification of compilation variants. The
value specified is available to the VARIANT preprocessor built-in function at compile time. The format of compilation
variants is:variant
-Tvariant{ [alphanumeric-string] } { ["alphanumeric-string"] } { ['alphanumeric-string'] }
For example, if you want to compile a program with one of three different INCLUDE files, you can use the -T
command qualifier to specify which file to include.variant
In the following example, the file SPECIAL.SRC is included in the program only if -T SPECIAL appears
in the pl1 command line:
%IF VARIANT() = 'SPECIAL' %THEN %INCLUDE 'SPECIAL.SRC'; %IF VARIANT() = 'NONE' %THEN;
No action is taken if -T NONE appears on the pli command line.
To specify a null variant, omit the -T option from the pli command line.
11.4.98. VERIFY
Dialects - ansi, dec, ibm
The VERIFY built-in function compares a string with a character-set string and verifies that all characters appearing in the string also appear in the character-set string. The function returns the value zero if they all appear. If not, the function returns a fixed-point binary integer that indicates the position of the first character in the string that is not present in the character-set string. The comparison is done character by character and left to right, and as soon as one nonmatching character is found in the first string, no more characters are compared. The function is case sensitive.
The format of the function is:
VERIFY(string,character-set-string[,starting-position])
stringA character-string expression representing the string to be checked.
character-set-stringA character-string expression containing the set of characters with which the characters in the first string are to be compared.
starting-positionA positive integer in the range 1 to n+1, where n is the length of the first string. It specifies the leftmost position in the first string to be compared with the
character-set-string. (By default, the comparison starts at the left end of the first string.)
Examples
STRING = 'HOW MUCH IS 1 PLUS 2'; ALPHABET = 'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ '; A = VERIFY(STRING,ALPHABET);
The value of the variable ALPHABET is a string containing the 26 lowercase letters, the 26 uppercase letters, and the space character. The function returns a value of 13, indicating the position of the character `1', which is the first nonalphabetic and nonspace character in STRING.
A = VERIFY(STRING,' ');
This example finds the first nonspace character in a string by using the space character as a test string. Note that constants can be used as the string parameters.
NEWSTRING = 'ALL LETTERS'; A = VERIFY(NEWSTRING,ALPHABET);
VERIFY returns a value of zero because all characters in the string NEWSTRING are present in the string ALPHABET.
NEWSTRING = '9 LETTERS'; A = VERIFY (NEWSTRING,ALPHABET,2);
The optional
starting-positionparameter specifies that the comparison begins at position 2 in NEWSTRING. VERIFY returns a value of zero because all characters beginning with the second character in the string NEWSTRING are present in the string ALPHABET. If thestarting-positionparameter had not been specified, VERIFY would have returned a value of 1, because the first character (`9') in NEWSTRING is not present in ALPHABET.
Dialects - ansi, dec
The WARN preprocessor built-in function returns the number of diagnostic warning messages issued during compilation up to that particular point in the source program. The format for the WARN built-in function is:
WARN();
The function returns a fixed result representing the number of compile-time warning messages that were issued up to the point at which the WARN built-in function was encountered.
11.5. Built-In Subroutines
Built-in subroutines are specific PL/I routines that provide various added capabilities. These routines can be used in a CALL statement. All arguments are evaluated as for normal subroutines.
The built-in subroutines are summarized in Table 11.2, “Summary of PL/I Built-In Subroutines”, according to the following functional categories:
Condition-handling built-in subroutines assist in performing condition-related operations.
File-control built-in subroutines perform file operations not supported by the standard PL/I language.
Record-locking built-in subroutines allow extended record-locking control in conjunction with the OPTIONS clause of the READ statement.
For more information on the PL/I subroutines in Table 11.2, “Summary of PL/I Built-In Subroutines”, see the Endless PL/I for POSIX Systems User's Manual.
| Category | Routine Reference | Action | Dialect |
|---|---|---|---|
| Condition-handling | RESIGNAL() | Allows more processing of a signal. | dec,ansi |
| File-control | FLUSH(f) | Forces flushing of all buffers for file f. | dec,ansi |
| REWIND(f) | Resets file f to the beginning. | dec,ansi | |
| Sorting | PLIRETC | Sets the PL/I return code, which the Sort program examines in input and output routines and the PLIRETV built-in function returns. | ibm |
| PLISRTA | An entry point of the Sort program, which sorts records with the system's sort
command. | ibm | |
| PLISRTB | An entry point of the Sort program, which sorts records with the system's sort
command. | ibm | |
| PLISRTC | An entry point of the Sort program, which sorts records with the system's sort
command. | ibm | |
| PLISRTD | An entry point of the Sort program, which sorts records with the system's sort
command. | ibm |
A pseudovariable can be used, in certain assignment contexts, in place of an ordinary variable reference. The following example assigns the character `A' to a one-character substring of S, beginning at the second character of S:
SUBSTR(S,2,1) = 'A';
A pseudovariable can be used wherever the following three conditions are true:
The syntax specifies a variable reference.
The context is one that explicitly assigns a value to the variable.
The context does not require the variable to be addressable.
The principal contexts in which pseudovariables are used are:
The left side of an assignment statement
The input target of a GET statement
Note that a pseudovariable cannot be used in preprocessor statements or in an argument list. In the following example, SUBSTR is not interpreted as a pseudovariable:
CALL P(SUBSTR(S,2,1));
Here, SUBSTR is interpreted as a built-in function reference, rather than as a pseudovariable. The actual argument passed to procedure P is a dummy argument containing the second character of string S.
Table 11.3, “Pseudovariables by Dialect” lists the pseudovariables and the dialect in which they are valid:
| Pseudovariable | Valid Dialect |
|---|---|
| INT | ansi, dec |
| ONSOURCE | ansi, dec, ibm |
| ONCHAR | ansi, dec, ibm |
| PAGENO | ansi, dec |
| POSINT | ansi, dec |
| STRING | ansi, dec, ibm |
| SUBSTR | ansi, dec, ibm |
| UNSPEC | ansi, dec, ibm |
The next sections describe these pseudovariables in alphabetic order.
11.6.1. INT Pseudovariable
Dialects - ansi, dec
The INT pseudovariable assigns a signed integer value to specified storage. The format is:
INT(reference[,position[,length]]) =expression;
referenceA reference to connected storage. This reference must not be an array, structure, or named constant. If position and length are not specified, the length of the referenced storage must not exceed 32 bits. If it exceeds 32 bits, a fatal run-time error results.
positionA positive integer value that denotes the position of the first bit in the field. If omitted, position defaults to 1, signifying the first bit of the storage denoted by reference. If specified, position must satisfy the following condition:
1 <= position <= size(reference)
where size(reference) is the length in bits of the storage denoted by reference. A position equal to size(reference) implies a zero-length field.
lengthAn integer value in the range 0 through 32 that specifies the length of the field. If omitted, length is the number of bits from the bit denoted by position through the end of the storage denoted by reference. If specified, length must satisfy the following condition:
0<= position <= size(reference) - position
where size(reference) is the length in bits of the storage denoted by reference.
The INT pseudovariable is valid only in an assignment statement. You cannot use it as the target of an input statement or in other instances where pseudovariables are normally acceptable.
The expression to be assigned to the pseudovariable is first converted to the data type FIXED BINARY (31). Then, the internal representation of the resulting integer value is assigned to the storage specified by the arguments to INT. If the representation of the value is too large for assignment to the storage, the most significant bits of the integer are removed and no error is signaled.
Examples
DECLARE F FLOAT INITIAL (123.45); INT(F,8,8) = 25; /* Alter the exponent */ PUT SKIP LIST (F); /* New value */
In this example, the INT pseudovariable is used to modify the exponent field of a floating-point variable. This example prints the following value:
9.5102418E-32
Proper interpretation of this result requires understanding of the internal representation of floating-point numbers. As such, this example is only valid on the Alpha hardware.
The next example demonstrates how the INT pseudovariable treats cases in which the value is too large for the specified storage:
INTOVER: PROCEDURE OPTIONS (MAIN); DECLARE I15 FIXED BINARY (15), I31 FIXED BINARY (31); ON FIXEDOVERFLOW PUT SKIP LIST ('FIXEDOVERFLOW signaled'); I31 = -876543; /* Too big for I15 */ I15 = I31; /* Arithmetic assignment */ INT(I15) = I31; /* No error signaled */ PUT SKIP LIST (I15); END;
This example produces the following output:
FIXEDOVERFLOW signaled
-24575The arithmetic assignment to I15 signals FIXEDOVERFLOW because the value of I31 is outside the range of a FIXED BINARY (15) variable. However, the assignment using the INT pseudovariable does not signal an error; it just copies the low-order 16 bits of the value of I31 into the storage for I15.
Dialects - ansi, dec, ibm
The ONCHAR pseudovariable can be used to replace the single character in the ONSOURCE value that caused a CONVERSION condition to be raised. An attempt to assign a value to the ONCHAR pseudovariable when there is no active CONVERSION condition causes the ERROR condition to be raised.
The format of the pseudovariable is:
ONCHAR()
See Section 8.10.4.6, “CONVERSION Condition” for more information about CONVERSION condition name.
11.6.3. ONSOURCE Pseudovariable
Dialects - ansi, dec, ibm
The ONSOURCE pseudovariable can be used to replace the entire ONSOURCE value that caused a CONVERSION condition to be raised. An attempt to assign a value to the ONSOURCE pseudovariable when there is no active CONVERSION condition causes the ERROR condition to be raised.
The format of the pseudovariable is:
ONSOURCE()
The ONSOURCE value is a fixed-length string value. An assignment of a longer string is truncated, and an assignment of a shorter string is padded with blanks on the right to the necessary length.
See Section 8.10.4.6, “CONVERSION Condition” for more information about CONVERSION condition name.
11.6.4. PAGENO Pseudovariable
Dialects - ansi, dec
The PAGENO pseudovariable refers to the page number of the referenced print file. Assignment to the pseudovariable modifies the current page number. The format of the PAGENO pseudovariable in an assignment statement is:
PAGENO(reference) =expression;
referenceA reference to a file for which the page number is to be set. The file must be open and must be a print file.
PAGENO(reference) is a FIXED BINARY(15) variable; however, values assigned to it must not be negative.
11.6.5. POSINT Pseudovariable
Dialects - ansi, dec
The POSINT pseudovariable assigns an integer value to specified storage. The format is:
POSINT(expression1[,position[,length]]) =expression2;
expression1A reference to connected storage. This reference must not be an array, structure, or named constant. If position and length are not specified, the length of the referenced storage must not exceed 32 bits. (If it exceeds 32 bits, a fatal run-time error results.)
positionA positive integer value that denotes the position of the first bit in the field. If omitted, position defaults to 1, signifying the first bit of the storage denoted by expression1. If specified, position must satisfy the following condition:
1 <= position <= size(expression1)
Size(expression1) is the length in bits of the storage denoted by expression1. A position equal to size(expression1) implies a zero-length field.
lengthAn integer value in the range 0 through 32 that specifies the length of the field. If omitted, length is the number of bits from the bit denoted by position through the end of the storage denoted by expression1. If specified, length must satisfy the following condition:
0<= position <= size(expression1) - position
Size(expression1) is the length in bits of the storage denoted by expression1.
expression2Any expression that evaluates to an integer.
The POSINT pseudovariable is valid only in an assignment statement. It cannot be used as the target of an input statement or in other instances where pseudovariables are normally acceptable.
The expression to be assigned to the pseudovariable is first converted to the data type FIXED BINARY (31). Then, the internal representation of the resulting integer value is assigned to the storage specified by the arguments to POSINT. If the representation of the value is too large for assignment to the storage, the most significant bits of the integer are removed and no error is signaled.
The POSINT pseudovariable is identical in operation and use to the INT pseudovariable. For examples, see INT pseudovariable.
11.6.6. STRING Pseudovariable
Dialects - ansi, dec, ibm
The STRING pseudovariable interprets a suitable reference as a reference to a fixed-length string. By using it, you can modify an entire aggregate with a single string assignment or assign the aggregate to a pictured variable as if it were a character-string variable. The format of the pseudovariable (in an assignment statement) is:
STRING(reference) =expression;
referenceA reference to a variable that is suitable for character-string (or bit-string) overlay defining (see Section 5.5.6.1, “Matching by Overlay Defining” and Section 5.8.2, “Rules for Overlay Defining”). The length of the pseudovariable is equal to the total number of characters (or bits) in the scalar or aggregate denoted by the reference. This length must be less than or equal to the maximum length for character-string (or bit-string) data.
Assignment to the STRING pseudovariable modifies the entire storage denoted by the reference.
Examples
STRING_PSD_EXAMPLE: PROCEDURE; DECLARE 1 NAME, 2 FIRST CHARACTER(10), 2 MIDDLE_INITIAL CHARACTER(3) 2 LAST CHARACTER(10); STRING(NAME)='FRANKLIN D. ROOSEVELT'; /* NAME.FIRST - 'FRANKLIN D'; NAME.MIDDLE_INITIAL = '. R'; NAME.LAST = 'OOSEVELT '; */ END STRING_PSD_EXAMPLE; ⋮ DECLARE 1 FLAGS, 2 (A,B,C) BIT(1); STRING(FLAGS) = '0'B; /* sets all three flags false */ ⋮ DECLARE P PICTURE /Z.ZZZV,ZZDB'; GET EDIT (STRING(P)) (A(10)); /* assigns 10 characters from SYSIN to P, without conversion */
11.6.7. SUBSTR Pseudovariable
Dialects - ansi, dec, ibm
The SUBSTR pseudovariable refers to a substring of a specified string variable reference. Assignment to the pseudovariable modifies only the substring. The format of the pseudovariable (in an assignment statement) is:
SUBSTR(reference,position[,length]) =expression;
referenceA reference to a bit- or character-string variable. If the reference is to a varying-length character string, the substring defined by the position and length arguments must be within the current value of the string. Assignment to the SUBSTR pseudovariable does not change the length of a varying string.
positionAn integer expression indicating the position of the first bit or character in the substring. The length must satisfy the following condition:
1 <= position <= LENGTH(reference) + 1
lengthAn integer expression that indicates the length of the substring. If unspecified, length is:
1 <= position <= LENGTH(reference) - position + 1
In other words, if length is not specified, the substring begins at the indicated position and ends at the end of the string. The length must satisfy the following condition:
length = LENGTH(reference) - position + 1
Note that the following two lines are equivalent:
SUBSTR(r,p,l) = v; r = SUBSTR(r,1,p-1)||SUBSTR(v||SUBSTR(r,p+length(v)),1,l) ||SUBSTR(r,p+l);
Assignment to the SUBSTR pseudovariable does not change the length of reference.
Examples
DECLARE (NAME,NEW_NAME) CHARACTER(20) VARYING; NAME = 'ISAK DINESEN'; NEW_NAME = NAME; SUBSTR(NEW_NAME,4) = 'AC NEWTON'; /* NEW_NAME = 'ISAAC NEWTON' */
11.6.8. UNSPEC Pseudovariable
Dialects - ansi, dec, ibm
The UNSPEC pseudovariable interprets a reference to a scalar or aggregate element variable as a reference to a bit string. The format of the pseudovariable (in an assignment statement) is:
UNSPEC(reference) =expression;
referenceA reference to a scalar or aggregate variable. The length of its storage in bits must be less than or equal to the maximum length for bit-string data.
In an assignment of the form
UNSPEC(
reference) =expression;the value of the expression is converted to a bit string if necessary and copied into the storage of the reference. The value is truncated or zero-extended as necessary to match the length of the storage.
Examples
DECLARE X FIXED BINARY (15); UNSPEC(X) = '110'B;
The use of the constant '110'b, which appears to be 6 in binary, actually assigns 3 to X. The two low-order bits of X (that is, X's first two bits of storage) are set; all other bits of X are cleared.
UNSPEC(X,1,3) = '101'B;
The optional parameters position and length are specified, causing the first three, low-order bits of the variable X to be assigned the value '101'B; the other bits are unaffected.