Re: Do loop -- replace with array ops?




PhilB wrote:
Simple question: can I make this neater? (ie replace the loop with array
operations)

Background:
we have the integer array subtemp(1:nsubtemp,1:3)
and the data structure s. subtemp contains data from a particular file,
and having read it in we want to put it into the data structure properly
(don't ask why I have to do it like this).
The relevant part of s is s%sub(1:nsub) where nsub<nsubtemp.

k=0
DO j=1,nsubtemp
IF( subtemp(j,1)==s%ifile )THEN
k=k+1
IF( k>s%nsub )THEN
(error handling thing)
END IF
s%sub(k)%ig = subtemp(j,2)
s%sub(k)%isub = subtemp(j,3)
END IF
END DO


I've tried a WHERE:
WHERE( subtemp(:,1)==s%ifile )
s%subh%ig = subtemp(:,2)
s%subh%isub = subtemp(:,3)
END WHERE
(won't work because the arrays aren't conformable, because nsub<nsubtemp.)

I've tried a FORALL:
FORALL( j=1:nsubtemp, k=1:xenosub(i)%nfsub, subtemp(j,1)==s%ifile )
s%subh(k)%ig = subtemp(j,2)
s%subh(k)%isub = subtemp(j,3)
END FORALL
(won't work because it uses every combination of j,k rather than one
unique k-value for every j that passes the mask)


Sorry if I'm being thick -- I just feel that I aught to be able to do
this neater than with a DO loop...

Thanks,

PhilB

Well, you can use

nmask = count(subtemp(:,1)==s%ifile)
IF (nmask > s%nsub) THEN
(error handling thing)
END IF
s%subh%ig(1:nmask) = PACK(subtemp(:,2),subtemp(:,1)==s%ifile)
s%subh%isub(1:nmask) = PACK(subtemp(:,3),subtemp(:,1)==s%ifile)

but I'd like to say that there is nothing wrong with DO loops in modern
Fortran,
and a lot of people would certainly consider the DO version more
readable,
not speaking of the fact that it has a good chance of being faster.

Jaroslav

.



Relevant Pages

  • Re: Do loop -- replace with array ops?
    ... we have the integer array subtemp ... subtemp contains data from a particular file, and having read it in we want to put it into the data structure properly. ...
    (comp.lang.fortran)
  • Re: Do loop -- replace with array ops?
    ... PhilB wrote: ... we have the integer array subtemp ... subtemp contains data from a particular file, and having read it in we want to put it into the data structure properly. ...
    (comp.lang.fortran)
  • Re: puzzle
    ... Darius wrote: ... there is an integer array whose length is odd, and all the numbers in the array appear exactly two times except one. ... Try to do it without using any other data structure. ...
    (comp.programming)