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 :: line=& aBcdef ghijklmnop qrstuvwxyz 1:|:2 333|333 a B cc character(len=:),allocatable :: array(:) ! output array of tokens write(*,*)INPUT LINE:[//LINE//] write(*,(70("="))) write(*,*)typical call: CALL split(line,array) write(*,(i0," ==> ",a))(i,trim(array(i)),i=1,size(array)) write(*,*)SIZE:,SIZE(array) write(*,(70("-"))) write(*,*)custom list of delimiters (colon and vertical line): CALL split(line,array,delimiters=:|,& & order=sequential,nulls=ignore) write(*,(i0," ==> ",a))(i,trim(array(i)),i=1,size(array)) write(*,*)SIZE:,SIZE(array) write(*,(70("-"))) write(*,*) custom list of delimiters, & &reverse array order and count null fields: CALL split(line,array,delimiters=:|,& &order=reverse,nulls=return) write(*,(i0," ==> ",a))(i,trim(array(i)),i=1,size(array)) write(*,*)SIZE:,SIZE(array) write(*,(70("-"))) write(*,*)INPUT LINE:[//LINE//] write(*,*) default delimiters and reverse array order & &and return null fields: CALL split(line,array,delimiters=,order=reverse,nulls=return) write(*,(i0," ==> ",a))(i,trim(array(i)),i=1,size(array)) write(*,*)SIZE:,SIZE(array) end program demo_splitOutput
>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 list of delimiters (colon and vertical line): >1 ==> aBcdef ghijklmnop qrstuvwxyz 1 >2 ==> 2 333 >3 ==> 333 a B cc > SIZE: 3 >---------------------------------------------------------------- > custom list of delimiters, reverse array order and return null fields: >1 ==> 333 a B cc >2 ==> 2 333 >3 ==> >4 ==> >5 ==> aBcdef ghijklmnop qrstuvwxyz 1 > SIZE: 5 >---------------------------------------------------------------- > INPUT LINE:[ aBcdef ghijklmnop qrstuvwxyz 1:|:2 333| 333 a B cc ] > default delimiters and reverse array order and count 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
split (3) | March 11, 2021 |