i have a question about a loop inside a subroutine in fortran.
If i put this as subroutine, then i expect that the variable test becomes an array from 1 to 5.
p.s. type3 is defined as real, dimension(5,1)
subroutine build(test)
type(typelist) :: test
do i = 1, 5
test%type3(i) = i
end subroutine build
However this gives an error ;
||Error: Rank mismatch in array reference (1/2)|
And when i remove the "(i)" after test%type3, it will work, but the result is 5.000 5.000 5.000 5.000 5.000. So it only assigns the value from the last loop to all entries in the array. And if i remove the %test the program does not know what type the variable test is anymore and it gives
||Error: Unclassifiable statement |
Can someone tell me what i am doing wrong?
type3 = iwill set all array elements equal to i, which is why you get all 5s (the last do loop iteration overwrites previous ones). You can omit all array indices this way. But you can't just omit some (like one in a rank-2 array). You could dotest%type3(i,:) = i(to broadcast over the last index) ortest%type3(i,1) = i, but you can't just omit array indices - in general, there's no way to tell which ones you're leaving out.