Syntax
DETACH expression variable [ { POSITION | START } expression ] [ DELIMITER expression ]
Parameters
Detach can be abbreviated as DET or DTH.
expression
A string value. It must be type compatible with the following qualifier and the delimiter expression (if specified).
variable
A string-primitive variable. It must be type compatible with the preceding expression, and the following delimiter expression (if specified).
POSITION or START expression
POSITION can be abbreviated as POS or PO. Specifies the position in the string expression of the first character of the substring to detach.
If not specified, 1 is used as the starting position, that is the beginning of the string.
The expression start value can be a number-primitive attribute, variable, or literal. It can be signed or unsigned but can not have decimals.
If an attribute is used to specify the starting position, its value will be updated. The new value is the position of the first non-delimiter character following the delimiter in the string expression.
DELIMITER expression
DELIMITER can be abbreviated as DELIM. Specifies the delimiting character of the substring to detach (the character following the last character of the substring). If a numeric value is used as a delimiter, it will be converted to text.
If not specified, a space character is used as the delimiter value.
The expression delimiter value can be a variable or literal.
Description
The Detach logic command moves text from an input value into a result variable of the same primitive type.
Note: For MCP-based systems, COBOL85 requires numeric data to be large enough to contain a value equal to one plus the size of the item being moved by the command. For example, if the item being moved is 99 characters long, numeric data must have a length of at least 3.
The Detach logic command sets the Glb.Length built-in segment attribute to the length of the text selected from the input variable.
Note: The Detach logic command does not set the Glb.Status built-in segment attribute.
The text to be detached is defined by a starting position and a delimiting character:
Starting position
The starting position is not validated. If it is invalid, the detach does not occur, and the result variable contains spaces.
If the start value is a variable, it is set to the position of the first non-delimiting character following the delimiter value in the new value of input variable.
Delimiting character
Multiple consecutive occurrences of the delimiter value are treated as a single occurrence. The delimiter value is not included in the detachment.
If the specified delimiter value does not occur in the substring of the input variable from the start value, the entire substring from the start value is detached to the result variable.
Data truncation
The Detach logic command can detach a substring of up to 2000 characters (1000 national string characters). However, the substring is truncated if the result variable is not sufficiently long.
If a national string value is input to a national string-primitive variable, the value is moved character-by-character, and truncated to the length of the variable.
If a national string value is input to a string-primitive variable, the value is moved byte-by-byte, and truncated to the length of the variable. This truncation might occur after the first byte of a double-byte character, resulting in an invalid character.
Examples
Example 1
This example detaches "Quick Brown Fox" from SD_Data1 and stores it in SD_Data2. The Glb.Length built-in segment attribute is set to 15.
SD_Data1 := "The Quick Brown Fox" Detach SD_Data1 SD_Data2 Position 5 Delimiter "."
Example 2
This example displays words in a sentence one by one, with their lengths.
SD_Ptr := 1 SD_Data1 := "The Quick Brown Fox" Loop Detach SD_Data1 SD_Word Position SD_Ptr Delimiter " " If SD_Word = Glb.Spaces Break End Message Glb.Length SD_Word End : SD_Ptr is updated to the next word as each word is detached, : so the loop displays the following messages: : 3 The : 5 Quick : 5 Brown : 3 Fox