Fortran cycle from the list

I use Fortran and I was wondering if it is possible to do something like this

  do i = array
    write (*,*) i
  end do

where array is a list of integers, not necessarily ordered.

+5
source share
2 answers

I would add a second index to iterate over the elements of the array:

program test

  implicit none

  integer, dimension(6)  :: A
  integer, dimension(10) :: B
  integer                :: i, j

  A = (/ 1, 3, 4, 5, 8, 9 /)
  B = (/ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 /)

  do j = 1, size(A)
     i = A(j)
     write(*,*) i, B(i)
  end do

end program test
+6
source

You want to say that you want to write some of the elements of an array with a name other_array, but not all of them, and that it ishould take essentially arbitrary values ​​in turn? In other words, you want to print not

do i = 1, size(other_array,1)
    write(*,*) other_array(i)
end do

but something like

array = [1,3,4,2,3,7,8,8,12]
write(*,*) another_array(array)

which will record the items another_arrayspecified in array? This is called an array substring. I have not tested this, and now I'm leaving, so I won’t.

+2
source

All Articles