2014-04-03 54 views
1

可能是一个愚蠢的问题,但有没有办法为变量的离散值运行循环?在最新版本中呢?离散值的Fortran循环

喜欢的东西

for i in 1 5 9 11 31 77 

在UNIX Shell脚本中使用?

谢谢。

+0

有人正试图在[tag:fotran]中编写[tag:pascal]程序。 – ja72

+0

我同意,应该可以说'DO I =(/ 1,3,5,7 /)',但你不能。也许它可以在[隐式做]中完成(http://stackoverflow.com/questions/4070528/implicit-do-loop-array-initialization)。我会调查。 – ja72

+0

@ ja72:你断言某人正试图在Fortran中编写一个Pascal程序很奇怪,Pascal没有比Fortran提供的更多的支持“循环”任意数字列表。此外,OP表明她从Unix shell脚本中汲取灵感。 –

回答

5
integer, dimension (5) :: indx = [5, 9, 11, 31, 71] 

do i=1, size(indx) 
    j=indx(i) 
    .... 
end do 
0

我不知道这会有所帮助,但你可以使用阵列来的indeces

program Console1 

implicit none 

! Variables 
INTEGER :: X(4) = (/ 1, 3, 5, 7 /) 
REAL :: Y(10) = 0.0 

! Body of Console1 
print *, X 
! 1 3 5 7 
Y(X) = 10.0/X 
print *, Y 
! 10.0 0.0 3.33 0.0 2.00 0.0 1.428 0.0 ... 

end program Console1 
0

你也可以使用一个隐含的DO循环来做到这一点,但你必须定义数组的值如上:

integer, dimension (5) :: indx = [5, 9, 11, 31, 71] 
integer, dimension (5) :: rslt 
integer, external  :: func 
rslt = (/ func(indx(j)), j=1,5 /)