split(3f) - [M_strings:TOKENS] parse string into an array using specified delimiters (LICENSE:PD)
Synopsis
Description
Options
Examples
Author
License
subroutine split(input_line,array,delimiters,order,nulls)
character(len=*),intent(in) :: input_line character(len=:),allocatable,intent(out) :: array(:) character(len=*),optional,intent(in) :: delimiters character(len=*),optional,intent(in) :: order character(len=*),optional,intent(in) :: nulls
SPLIT(3f) parses a string using specified delimiter characters and store tokens into an allocatable array
INPUT_LINE Input string to tokenize ARRAY Output array of tokens DELIMITERS List of delimiter characters. The default delimiters are the "whitespace" characters (space, tab,new line, vertical tab, formfeed, carriage return, and null). You may specify an alternate set of delimiter characters. Multi-character delimiters are not supported (Each character in the DELIMITERS list is considered to be a delimiter).
Quoting of delimiter characters is not supported.
ORDER SEQUENTIAL|REVERSE|RIGHT Order of output array. By default ARRAY contains the tokens having parsed the INPUT_LINE from left to right. If ORDER=RIGHT or ORDER=REVERSE the parsing goes from right to left. (This can be accomplished with array syntax in modern Fortran, but was more useful pre-fortran90). NULLS=IGNORE|RETURN|IGNOREEND Treatment of null fields. By default adjacent delimiters in the input string do not create an empty string in the output array. if NULLS=return adjacent delimiters create an empty element in the output ARRAY. If NULLS=ignoreend then only trailing delimiters at the right of the string are ignored.
Sample program:
program demo_split use M_strings, only: split implicit none integer :: i character(len=*),parameter :: title=(80("="),t1,a) character(len=*),parameter :: line=& aBcdef ghijklmnop qrstuvwxyz 1:|:2 333|333 a B cc character(len=:),allocatable :: array(:) ! output array of tokens write(*,*)INPUT LINE:[//line//] ! write(*,title)typical call: call split(line,array) call printme() ! write(*,title)custom delimiters=":|" : call split(line,array,delimiters=:|,& & order=sequential,nulls=ignore) call printme() ! write(*,title)& delimiters=":|",reverse array order and count null fields: call split(line,array,delimiters=:|,& & order=reverse,nulls=return) call printme() ! write(*,title)& default delimiters, reverse array order and return null fields: call split(line,array,delimiters=,& & order=reverse,nulls=return) call printme() contains subroutine printme() write(*,(i0," ==> ",a))(i,trim(array(i)),i=1,size(array)) write(*,*)SIZE:,size(array) end subroutine printme end program demo_splitResults:
> INPUT LINE: > [ aBcdef ghijklmnop qrstuvwxyz 1:|:2 333|333 a B cc ] > typical call: ======================================================== > 1 ==> aBcdef > 2 ==> ghijklmnop > 3 ==> qrstuvwxyz > 4 ==> 1:|:2 > 5 ==> 333|333 > 6 ==> a > 7 ==> B > 8 ==> cc > SIZE: 8 > custom delimiters=":|" : ============================================= > 1 ==> aBcdef ghijklmnop qrstuvwxyz 1 > 2 ==> 2 333 > 3 ==> 333 a B cc > SIZE: 3 > delimiters=":|",reverse array order and count null fields:============ > 1 ==> 333 a B cc > 2 ==> 2 333 > 3 ==> > 4 ==> > 5 ==> aBcdef ghijklmnop qrstuvwxyz 1 > SIZE: 5 > default delimiters, reverse array order and return null fields:======= > 1 ==> > 2 ==> > 3 ==> > 4 ==> cc > 5 ==> B > 6 ==> a > 7 ==> 333|333 > 8 ==> > 9 ==> > 10 ==> > 11 ==> > 12 ==> 1:|:2 > 13 ==> > 14 ==> qrstuvwxyz > 15 ==> ghijklmnop > 16 ==> > 17 ==> > 18 ==> aBcdef > 19 ==> > 20 ==> > SIZE: 20
John S. Urban
Public Domain
Nemo Release 3.1 | split (3m_strings) | January 10, 2025 |