chomp(3f) - [M_strings:TOKENS] Tokenize a string, consuming it one token per call (LICENSE:PD)
Synopsis
Description
Options
Returns
Examples
Author
License
function chomp(source_string,token[,delimiters])
character(len=*) :: source_string character(len=:),intent(out) :: token character(len=:),intent(in),optional :: delimiters integer :: chomp
The CHOMP(3f) function is used to isolate sequential tokens in a string, SOURCE_STRING. These tokens are delimited in the string by at least one of the characters in DELIMITERS. This routine consumes the source_string one token per call. It returns -1 when complete. The default delimiter list is "space,tab,carriage return,newline".
SOURCE_STRING string to tokenize DELIMITERS list of separator characters
TOKEN returned token CHOMP status flag. 0 = success, -1 = no tokens remain
Sample program:
program demo_chompsample input fileuse M_strings, only : chomp implicit none character(len=100) :: inline character(len=:),allocatable :: token character(len=*),parameter :: delimiters= ;, integer :: iostat integer :: icount integer :: itoken icount=0 do ! read lines from stdin until end-of-file or error read (unit=*,fmt="(a)",iostat=iostat) inline if(iostat /= 0)stop icount=icount+1 itoken=0 write(*,*)INLINE ,trim(inline) do while ( chomp(inline,token,delimiters) >= 0) itoken=itoken+1 print *, itoken,TOKEN=[//trim(token)//] enddo enddo
end program demo_chomp
this is a test of chomp; A:B :;,C;;sample output file
> INLINE this is a test of chomp; A:B :;,C;; > 1 TOKEN=[this] > 2 TOKEN=[is] > 3 TOKEN=[a] > 4 TOKEN=[test] > 5 TOKEN=[of] > 6 TOKEN=[chomp] > 7 TOKEN=[A:B] > 8 TOKEN=[:] > 9 TOKEN=[C]
John S. Urban
Public Domain
Nemo Release 3.1 | chomp (3m_strings) | January 10, 2025 |