sep(3f) - [M_strings:TOKENS] function to parse string into an array using
specified delimiters
(LICENSE:PD)
function sep(input_line,delimiters,nulls)
character(len=*),intent(in) :: input_line
character(len=*),optional,intent(in) :: delimiters
character(len=*),optional,intent(in) :: nulls
character(len=:),allocatable :: sep(:)
sep(3f) parses a string using specified delimiter characters and
store tokens into an allocatable array
INPUT_LINE Input string to tokenize
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.
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.
ORDER='ASCENDING'|'DESCENDING' by default the tokens are returned from
last to first; order='ASCENDING' returns
them from first to last (left to right).
SEP Output array of tokens
Sample program:
program demo_sep
use M_strings, only: sep
character(len=*),parameter :: fo='(/,a,*(/,"[",g0,"]":,","))'
character(len=*),parameter :: line=&
' aBcdef ghijklmnop qrstuvwxyz 1:|:2 333|333 a B cc '
write(*,'(a)') 'INPUT LINE:['//LINE//']'
write(*,fo) 'typical call:',sep(line)
write(*,fo) 'delimiters ":|":',sep(line,':|')
write(*,fo) 'count null fields ":|":',sep(line,':|','return')
end program demo_sep
Output
INPUT LINE:[ aBcdef ghijklmnop qrstuvwxyz 1:|:2 333|333 a B cc ]
typical call:
[cc ],
[B ],
[a ],
[333|333 ],
[1:|:2 ],
[qrstuvwxyz],
[ghijklmnop],
[aBcdef ]
delimiters ":|":
[333 a B cc ],
[2 333 ],
[ aBcdef ghijklmnop qrstuvwxyz 1]
count null fields ":|":
[333 a B cc ],
[2 333 ],
[ ],
[ ],
[ aBcdef ghijklmnop qrstuvwxyz 1]
John S. Urban
Public Domain
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | input_line | |||
character(len=*), | intent(in), | optional | :: | delimiters | ||
character(len=*), | intent(in), | optional | :: | nulls | ||
character(len=*), | intent(in), | optional | :: | order |
function sep(input_line,delimiters,nulls,order)
!-----------------------------------------------------------------------------------------------------------------------------------
! ident_7="@(#) M_strings sep(3f) parse string on delimiter characters and store tokens into an allocatable array"
! John S. Urban
!-----------------------------------------------------------------------------------------------------------------------------------
intrinsic index, min, present, len
!-----------------------------------------------------------------------------------------------------------------------------------
! given a line of structure " par1 par2 par3 ... parn " store each par(n) into a separate variable in array.
! o by default adjacent delimiters in the input string do not create an empty string in the output array
! o no quoting of delimiters is supported
character(len=*),intent(in) :: input_line ! input string to tokenize
character(len=*),optional,intent(in) :: delimiters ! list of delimiter characters
character(len=*),optional,intent(in) :: nulls ! return strings composed of delimiters or not ignore|return|ignoreend
character(len=*),optional,intent(in) :: order ! return strings composed of delimiters or not ignore|return|ignoreend
character(len=:),allocatable :: sep(:) ! output array of tokens
integer :: isize
call split(input_line,sep,delimiters,'right',nulls)
if(present(order))then
select case(order)
case('ascending','ASCENDING')
isize=size(sep)
if(isize > 1)then
sep=sep(isize:1:-1)
endif
end select
endif
!-----------------------------------------------------------------------------------------------------------------------------------
end function sep