0

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
10
  • show the declaration for tu in the main prpgram. Commented Feb 6, 2015 at 4:43
  • 2
    You do know that even a list-directed internal read of the string will get the integers into an array for you, don't you ? Commented Feb 6, 2015 at 7:04
  • Can confirm it is a gcc problem when using array ranges into a polymorphic objects. A problem with the offset being 0 rather than 1. Commented Feb 6, 2015 at 13:01
  • @HighPerformanceMark. Can you elaborate on list-directed internal read please. Commented Feb 6, 2015 at 14:15
  • If you want to claim there is a fundamental "problem" with gcc please provide a complete and preferably minimal example. Commented Feb 6, 2015 at 15:51

1 Answer 1

2

To elaborate on my earlier comment. (I don't have Fortran on this computer so the syntax may be a little wonky.) What I meant is that something like this

integer, dimension(6), allocatable :: arr
integer :: ios
character(len=:), allocatable :: str
...
arr = 0
str = "1,2,3,5,8"
read(str,*,iostat=ios) arr

will read the first 6 integers from str into the elements of arr. As it happens str only has 5 integers so the last element of arr is left as 0. iostat is necessary here because an attempt to read more integers than str provides will otherwise produce an end-of-file error at run time.

Of course, this approach generalises to reals, characters and logicals too. Fortran has, built-in, polymorphic reading of intrinsic types. Up to a point.

Fortran will recognise blanks and spaces as value-separators (the Fortran standard uses the word delimiter to mean something else). Note that by setting the decimal mode on an i/o statement to comma Fortran will recognise the comma as a decimal point and treat a semi-colon, ;, as a value-separator. Technically a slash is also a value-separator but it also causes termination of list-directed input of the record so doesn't really act like a value-separator.

If concerned about strings containing other value-separators I might write a function which took a string containing such, returned one with only whitespace, and then perform the internal read on that.

Sign up to request clarification or add additional context in comments.

2 Comments

How does fortran define the delimiter?
fair point, if you want to handle arbitrary delimiters you would likely end up with something like your original approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.