Parse space-separated integers from a string into an allocatable array
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | str | |||
| integer, | intent(out), | allocatable | :: | values(:) |
subroutine parse_space_separated_ints(str, values) !! Parse space-separated integers from a string into an allocatable array implicit none character(*), intent(in) :: str integer, allocatable, intent(out) :: values(:) integer :: i, stat, ival character(256) :: work character(32) :: token work = trim(adjustl(str)) allocate(values(0)) do while(len_trim(work) .gt. 0) i = index(trim(work), ' ') if(i .eq. 0)then token = trim(work) work = '' else token = work(1:i-1) work = adjustl(work(i+1:)) end if read(token, *, iostat=stat) ival if(stat .eq. 0) values = [values, ival] end do end subroutine parse_space_separated_ints