I provide an example when converting a string to an integer array. I pass an array using an index range.The offset is being initiated by 0 rather than one, so the values in the array are being shifted.
s = "1,2,3,5,8"
Call str_to_num_tu (s, ",", tu(1:8))
$ Output:
$ tu(1): 0 ; tu(2): 1 ; tu(3): 2
Call str_to_num_tu (s, ",", tu)
$ Output:
$ tu(i): 1 ; tu(2): 2 ; tu(3): 3
Here is my subroutine using an unlimited polymorphic variable.
Subroutine str_to_num_tu &
( &
s, dl, tu, pos &
)
Class(*), Intent (InOut) :: tu(:)
Character (len=*), Intent (In) :: s, dl
Character (len=*), Intent (In), Optional :: pos
Integer, Allocatable :: ipos(:)
Integer :: nf, npos, ip, i, j, k
!!$ Sets tu.
!!$ s = "Pablo/Neruda"; tu = ["Pablo","Neruda"]
!!$ s = "0.2/1.3/1.5"; tu = [0.2,1.3,1.5]
nf = nfields (s, dl)
Write (*,*) ""
Write (*,*) "nf: ", nf, "; Size(tu): ", Size(tu)
i = 1
Do k = 1, nf-1
j = Index (s(i:), dl)
Select Type (tu)
Type Is (Integer (Int32))
Call str_to_num (s(i:i+j-2), tu(k))
Write (*,*) Trim (s(i:))
Write (*,*) "k: ", k, "; tu(k): ", tu(k)
End Select !!$ tu
i = i + j
End Do
!!$ Gets last field.
j = Index (s, dl, back=.true.)
Write (*,*) "j:", j, "; nf:", nf
Select Type (tu)
Type Is (Integer (Int32))
Call str_to_num (s(j+1:), tu(nf))
End Select !!$ tu
End Subroutine str_to_num_tu