delim(3f) - [M_strings:TOKENS] parse a string and store tokens into an array (LICENSE:PD)
Synopsis
Description
Options
Examples
Author
License
subroutine delim(line,array,n,icount,ibegin,iterm,lgth,dlim)
character(len=*),intent(in) :: line integer,integer(in) :: n integer,intent(out) :: icount character(len=*) :: array(n) integer,intent(out) :: ibegin(n) integer,intent(out) :: iterm(n) integer,intent(out) :: lgth character(len=*) :: dlim
Given a LINE of structure " par1 par2 par3 ... parn " store each par(n) into a separate variable in ARRAY (UNLESS ARRAY(1) == #N#)Also set ICOUNT to number of elements of array initialized, and return beginning and ending positions for each element in IBEGIN(N) and ITERM(N).
Return position of last non-blank character (even if more than N elements were found) in lgth
No quoting or escaping of delimiter is allowed, so the delimiter character can not be placed in a token.
No checking for more than N parameters; If any more they are ignored.
This routine originates pre-Fortran90. A version using optional parameters and allocatable arrays is on the TODO list.
LINE input string to parse into tokens ARRAY(N) array that receives tokens N size of arrays ARRAY, IBEGIN, ITERM ICOUNT number of tokens found IBEGIN(N) starting columns of tokens found ITERM(N) ending columns of tokens found LGTH position of last non-blank character in input string LINE DLIM delimiter characters
Sample program:
program demo_delimResults:use M_strings, only: delim implicit none character(len=80) :: line character(len=80) :: dlm integer,parameter :: n=80 character(len=20) :: array(n)= integer :: ibegin(n),iterm(n) integer :: i20, icount, lgth, i10,i30 line= first second 10.3 words_of_stuff do i20=1,4 ! change delimiter list and what is calculated or parsed if(i20 == 1)dlm= if(i20 == 2)dlm=o if(i20 == 3)dlm= aeiou ! NOTE SPACE IS FIRST if(i20 == 3)ARRAY(1)=#N# ! QUIT RETURNING STRING ARRAY if(i20 == 4)line=AAAaBBBBBBbIIIIIi J K L
! write out a break line composed of =========== .. write(*,(57("="))) ! show line being parsed write(*,(a))PARSING=[//trim(line)//] on //trim(dlm) ! call parsing procedure call delim(line,array,n,icount,ibegin,iterm,lgth,dlm) write(*,*)number of tokens found=,icount write(*,*)last character in column ,lgth if(icount > 0)then if(lgth /= iterm(icount))then write(*,*)ignored from column ,iterm(icount)+1, to ,lgth endif do i10=1,icount ! check flag to see if ARRAY() was set if(array(1) /= #N#)then ! from returned array write(*,(a,a,a),advance=no)& &[,array(i10)(:iterm(i10)-ibegin(i10)+1),] endif enddo ! using start and end positions in IBEGIN() and ITERM() write(*,*) do i10=1,icount ! from positions in original line write(*,(a,a,a),advance=no)& &[,line(ibegin(i10):iterm(i10)),] enddo write(*,*) endif enddo line=four score and seven years ago call delim(line,["#N#"],n,icount,ibegin,iterm,lgth, ) do i30=1,icount write(*,*)ibegin(i30),iterm(i30),& & [//line(ibegin(i30):iterm(i30))//] enddo
end program demo_delim
> ========================================================= > PARSING=[ first second 10.3 words_of_stuff] on > number of tokens found= 4 > last character in column 34 > [first][second][10.3][words_of_stuff] > [first][second][10.3][words_of_stuff] > ========================================================= > PARSING=[ first second 10.3 words_of_stuff] on o > number of tokens found= 4 > last character in column 34 > [ first sec][nd 10.3 w][rds_][f_stuff] > [ first sec][nd 10.3 w][rds_][f_stuff] > ========================================================= > PARSING=[ first second 10.3 words_of_stuff] on aeiou > number of tokens found= 10 > last character in column 34 > > [f][rst][s][c][nd][10.3][w][rds_][f_st][ff] > ========================================================= > PARSING=[AAAaBBBBBBbIIIIIi J K L] on aeiou > number of tokens found= 5 > last character in column 24 > > [AAA][BBBBBBbIIIII][J][K][L] > 1 4 [four] > 9 13 [score] > 15 17 [and] > 21 25 [seven] > 28 32 [years] > 34 36 [ago]================================================================================
John S. Urban
Public Domain
Nemo Release 3.1 | delim (3m_strings) | January 10, 2025 |