Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

SPLIT

Pure Intrinsic Subroutine (Generic): Locates the next delimiter in a string.

CALL SPLIT (string, set, pos [, back])

string

(Input) Must be a scalar of type character.

set

(Input) Must be a scalar with the same kind type parameter as string.

pos

(Input) Must be a scalar of type integer.

back

(Input; optional) Must be a scalar of type logical.

The characters in set are token delimiters. A token is any sequence of zero or more characters in string delimited by the beginning or end of string, or by any token delimiter in set. Two consecutive token delimiters in string, or a token delimiter in the first or last character position of string, comprise a zero-length token.

If back is not present, or if it is present with the value .FALSE., the scan begins at character position pos + 1 in string, scanning left to right until a character that matches one of the characters in set is found, or the end of string is reached. On input, the value of pos must be in the range 0 <= pos < LEN (string). If a match is found, pos is assigned the index of the matching delimiter in string. If no match is found, pos is assigned the value LEN (string) + 1. This identifies a token beginning at a position one greater than the value of pos on input, and ending at a position of one less than the value of pos on output.

If back is present with the value .TRUE., the scan begins at the character position pos - 1 in string, scanning right to left until a character that matches one of the characters in set is found, or the beginning of string is reached. On input, the value of pos must be in the range 0 < pos <= LEN (string) + 1. If a match is found, pos is assigned the index of the matching character delimiter in string. If no match is found, pos is assigned the value 0. This identifies a token beginning at a position in string of one greater than the value of pos on output, and ending at a position of one less than the value of pos on input.

If SPLIT is called with a value of pos in the range 1 <= pos <= LEN (string) and string (pos:pos) does not match any character in set, meaning that SPLIT was called with pos pointing to a non-delimiter character in string, the resulting token identified by SPLIT will be a partial token and will not be a complete token as described above.

Examples

Consider the following:

INTEGER         :: first, last, pos, length
CHARACTER(LEN=3):: delims = '=|;'
CHARACTER(LEN=:),ALLOCATABLE :: words  
words = 'person=woman|man|child;camera=tv|security|phone'
length = LEN (words)
pos = 0
DO WHILE (pos < length)
    first = pos + 1
    call SPLIT (words, delims, pos)
    last = pos – 1
    print *, words (first:last)
END DO

The above will cause the compiler to print:

person
woman
man
child
camera
tv
security
phone

Consider the following:

INTEGER         :: first, last, pos, length
CHARACTER(LEN=3):: delims = '=|;'
CHARACTER(LEN=:),ALLOCATABLE :: words
words  = 'person=woman|man|child;camera=tv|security|phone'
pos    = LEN (words)+ 1
DO WHILE (pos > 0)
    last = pos - 1
    call SPLIT (words, delims, pos, .TRUE.)
    first = pos + 1
    print *, words (first:last)
END DO

The above will cause the compiler to print:

phone
security
tv
camera
child
man
woman
person

See Also