Yeah, right.
I think @snano understood the illustration I provided toward a possible approach solution.
But for any other reader if they want to overlook the bigger picture and the outline but would rather sweat the details, first please note the following:
- string manipulation toward such needs becomes much easier for users via a higher degree of abstraction, say a featured intrinsic string derived type (“class”) or a library solution toward such a type. Many modern and popular languages enable such an approach more natively than Fortran does currently and that is unfortunate. No wonder then OP and others in forum posts inquire re: Python, etc. and the convenient options with Fortran.
- should 1) be of not interest, one can proceed with intrinsic character type and work with it. Toward this,
TOKENIZEfrom Fortran 202X will become a good option once compiler support becomes a reality. In the meantime, one can roll one’s own procedure toward the same, as shown in this thread and elsewhere. It is not difficult.
Toward 1, I illustrated an approach but was unable to share further details. As to the mind-numbing minutiae brought up in the quoted comment here, again the message is users (say @snano) can take the illustration and adapt it as needed. What I showed was by no means meant to be prescriptive. As to “val”, “%s”, formats, etc., readers may know the anatomy of the approach is as follows:
module string_m
type :: string_t
private
character(len=:), allocatable :: m_s
contains
private
procedure, pass(this) :: assign_t
procedure, pass(this), public :: s => get_s
generic, public :: assignment(=) => assign_t
end type
contains
subroutine assign_t( this, s )
class(string_t), intent(inout) :: this
character(len=*), intent(in) :: s
this%m_s = s
end subroutine
function get_s( this ) result(r)
class(string_t), intent(in) :: this
character(len=:), allocatable :: r
r = this%m_s
end function
end module
use string_m, only : string_t
type(string_t) :: dat
real :: x
dat = "4.567" ! Simulate the loading of values as string tokens
associate ( val => dat%s() )
read( val, fmt="(f5.3)" ) x
print *, x
end associate
end
With IFORT,
C:\Temp>ifort /standard-semantics p.f90
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.6.0 Build 20220226_000000
Copyright (C) 1985-2022 Intel Corporation. All rights reserved.
Microsoft (R) Incremental Linker Version 14.32.31329.0
Copyright (C) Microsoft Corporation. All rights reserved.
-out:p.exe
-subsystem:console
p.obj
C:\Temp>p.exe
4.567000
Now, with the internal IO shown in this example, users would do well to consider suitable formats that can generally work with their data, or even list-directed read (fmt=*). Don’t get hung up on the shown format (F5.3), please.
And I believe error handling such as with iostat is best elided in illustrative examples, that is up to the users to employ to develop robust programs for their needs.