Re: Problem with my split routine I am not understanding



Jeremy wrote:
I have the following function:

subroutine split(what, by, into)
implicit none

character(len=*), intent(in) :: what, by
character(len=*), dimension(:), intent(out) :: into

integer :: bpos = 1, pos = 1, idx = 1

I haven't tried to understand your program, but, as a first
guess: This isn't what you want!
Fortran initialization is different from other languages. It's
a one-time initialization, usually done at load time, not every
time a routine is entered. You probably want
integer :: bpos, pos, idx
and then
bpos = 1
pos = 1
idx = 1

as executable statements.

*** Hendrickson
do
pos = scan(what(bpos:), by)
if (pos == 0) exit
into(idx) = what(bpos:pos + bpos - 2)
idx = idx + 1
bpos = bpos + pos
end do

into(idx) = what(bpos:)

end subroutine split

To test this, I have:

program SplitTest
uses splitmod
implicit none

character(10), dimension(1:2) :: data

call split('JOHN|DOE', '|', data)
print *, data(1) ! outputs: JOHN

call split('JANE|SMITH', '|', data)
print *, data(1) ! outputs: JANE
end program SplitTest

The problem is that once the array data is filled, it seems to not change.
Any ideas on what I am doing wrong?

Jeremy
.