How does one extract a substring of a Fortran string array? For example
program testcharindex
implicit none
character(len=10), dimension(5) :: s
character(len=10), allocatable :: comp(:)
integer, allocatable :: i(:), n(:)
s = (/ '1_E ', '2_S ', '3_E ', '14_E', '25_S' /)
i = index(s,'_')
print *,' i = ', i
n = s(1:i-1) ! or n = s(:i-1)
comp = s(i+1:)
print *,' n = ', n
print *,' comp = ', comp
end program
Compiling with gfortran yields the error:
testcharindex.f90:11:10:
n = s(1:i-1) 1 Error: Array index at (1) must be scalar
Is there any way of avoiding a do loop here? If one can extract an index of a string array, I would expect that one should be able to extract a dynamically-defined substring of a string array (without looping over the array elements). Am I too optimistic?