From: analyst41 on
On Oct 7, 10:39 am, Bart Vandewoestyne
<MyFirstName.MyLastN...(a)telenet.be> wrote:
> I have config files with lines that look as follows:
>
> on 0.2 0.2  0.02 0.02  0.01 0.01 pulse 5.0e8 100e3
>
> The number of spaces between items is arbitrary, and the number
> formatting is also arbitrarily chosen by the user.  The number of
> numbers after 'pulse' can vary.
>
> I read this as:
>
>     character(len=100) :: pointsource_string
>     ...
>     read(unit=my_unit, fmt="(A)", iostat=ios) pointsource_string
>
> But now I want to split pointsource_string in two, namely so that
> the two resulting strings are:
>
>   on 0.2 0.2  0.02 0.02  0.01 0.01 pulse
>
> and
>
>   5.0e8 100e3
>
> I have looked into my Fortran 95/2003 explained handbook,
> searching for a way to skip 7 'whitespace areas'... but i could
> not come up with an elegant method to split the string at the
> point where I want to split.
>
> Any suggestions on how to do this?
>
> Thanks,
> Bart
>
> --
>         "Share what you know.  Learn what you don't."

How about this?


character cscr*100,onvar*2,pulsevar*5
dimension var1(6),var2(1000)
cscr = 'on 0.2 0.2 0.02 0.02 0.01 0.01 pulse 5.0e8 100e3'



var2=-1


read (cscr,*,end=100,err=100) onvar,(var1(j),j=1,6),pulsevar,(var2
(j),j=1,1000)

100 continue

print *,trim(onvar),var1,trim(pulsevar),var2(1:5)

stop
end

output:

on 0.200000003 0.200000003 1.99999996E-02 1.99999996E-02
9.99999978E-03 9.999999
78E-03 pulse 500000000. 100000.000 -1.00000000 -1.00000000 -1.00000000
Program Completed
Press Enter to Continue.

initialize var2 to soemthing that will not occr in the input.

This is a bit dumb, but I don't know how to capture the value of j
when EOF,ERR or EOR occurs in the second implied do-loop. But the
first occrrence of -1 will tell you when input data stops.
From: David Kinniburgh on

>
>   subroutine find_field (string, field, position, delims, delim, found)
>
>     !-- Find a delimitted field in a string.
>     !-- 15 Nov 90, Richard Maine.
>

Richard's parser is nice but would be even more useful if it dealt
with quoted strings, i.e. strings containing blanks etc.
From: dpb on
David Kinniburgh wrote:
>> subroutine find_field (string, field, position, delims, delim, found)
>>
>> !-- Find a delimitted field in a string.
>> !-- 15 Nov 90, Richard Maine.
>>
>
> Richard's parser is nice but would be even more useful if it dealt
> with quoted strings, i.e. strings containing blanks etc.

Don't see why it wouldn't as long as used a unique delimiter (which
would have to do, anyway).

--
From: dpb on
David Kinniburgh wrote:
>> subroutine find_field (string, field, position, delims, delim, found)
>>
>> !-- Find a delimitted field in a string.
>> !-- 15 Nov 90, Richard Maine.
>>
>
> Richard's parser is nice but would be even more useful if it dealt
> with quoted strings, i.e. strings containing blanks etc.

OK, I put Richard's routine in a test program as follows--

module strings_package
contains
subroutine find_field (string, field, position, delims, delim, found)
! code elided here for brevity
end subroutine find_field
end module strings_package

program main
use strings_package
implicit none

character*80 :: s
character*80 :: f

integer :: posn
integer :: i

s = 'This,is a,comma-delimited,string,with a,space'

posn = 1
do i = 1,6
call find_field(s, f, posn, ',')
write(*,*) f(1:len_trim(f))
enddo
end program main


The output is (as I had expected)...

This
is a
comma-delimited
string
with a
space

--
From: David Kinniburgh on

>    s = 'This,is a,comma-delimited,string,with a,space'


yes, I see that. I was thinking of something like

s = '10 This "quoted string" that'

with space delimiters.