Detach

Syntax

DETACH expression variable [ { POSITION | START } expression ] 
[ DELIMITER expression ]

Parameters

Detach can be abbreviated as DET or DTH.

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:

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