Domain: String processing
The EXTRACT-FIELD( ) function extracts a field from an input string.
Format
EXTRACT-FIELD( ) |
STRING = string_expression ,FIELD-NUMBER = arithm_expression ,FIELD-SEPARATOR = *ANY-BLANKS / string_expression |
Result type
STRING
Input parameters
STRING = string_expression
Designates an input string.
FIELD-NUMBER = arithm_expression
Designates the field number.
FIELD-SEPARATOR =
Specifies the separator. Separators are not part of the extracted field.
FIELD-SEPARATOR = *ANY-BLANKS
The default value for the separator is one or more blanks.
(The specification of ' *
’ (two blanks in front of the *) is supported as compatible.)
FIELD-SEPARATOR = string_expression
string_expression is the separator.
Here however, string_expression must be a simple regular expression (for further details see “POSIX Commands” [18]).
Result
The extracted field, in the form of a string. If the specified field does not exist, the function returns a null string.
Error messages
SDP0472 NULL BYTE (X'00') NOT ALLOWED IN STRING AND FIELD SEPARATOR OPERANDS SDP0474 SYNTAX ERROR IN REGULAR EXPRESSION FOR OPERAND FIELD SEPARATOR SDP0485 VALUE OF OPERAND 'FIELD NUMBER' MUST BE GREATER THAN ZERO
Examples
Example 1
/DECLARE-VARIABLE mylist(TYPE=*STRING),MULTIPLE-ELEMENT=*LIST /mylist = 'Pencil 100',WRITE-MODE=*EXTEND /mylist = 'Table 5',WRITE-MODE=*EXTEND /mylist = 'Lamp 20',WRITE-MODE=*EXTEND /mylist = 'Paper 75',WRITE-MODE=*EXTEND /mylist = 'DVD 1000',WRITE-MODE=*EXTEND /mylist = 'Envelope 1500',WRITE-MODE=*EXTEND /FOR x = *LIST(mylist) / article = EXTRACT-FIELD(STRING=x,FIELD-NUMBER=1) / quantity = EXTRACT-FIELD(STRING=x,FIELD-NUMBER=2) / WRITE-TEXT '&quantity of &article are available' /END-FOR
Output:
100 of Pencil are available 5 of Table are available 20 of Lamp are available 75 of Paper are available 1000 of DVD are available 1500 of Envelope are available
Example 2
/A=EXTRACT-FIELD(STRING='field1,field3,field4',FIELD-NUMBER=3,FIELD-SEPARATOR=',') /SHOW-VARIABLE A A = field4