如何将数组的上下文传递给另一个数组?

时间:2019-01-26 01:18:55

标签: arrays fortran

我试图了解fortran如何将数组的部分上下文传递给另一个。

“ myarray”具有8个维度(或3个值中的8个),“ myarray1”具有1个维度(或3个值中的一个)。 myarray的一个选定上下文将具有3个值之一,并且应该能够将该上下文传递给myarra1。但是,fortran无法将其转换并传递。

function function1(start) result(out1)

implicit none

type array
 character  list1
 integer    list2
 integer    list3
end type array

type(array), dimension(8) :: myarray
               integer :: start;
               integer :: out1;


myarray=[array('A',1,3), array('C',2,3), array('B',3,3), & 
         array('E',4,3), array('F',4,3), array('G',5,3), & 
         array('H',6,3), array('I',5,3)]

out1=function2(start);

contains

function function2(list2) result(list1)

implicit none

type array1
 character  lista
 integer    list21
 integer    list31
end type array1

type(array1), dimension(1) :: myarray1

               integer :: start1;
               integer :: out11;

 integer, intent(in) :: list2;             ! input
 integer :: list1;                         ! output  
 logical :: done;  
 integer, dimension(1,1) :: ra;
 real    :: rnd1;
 integer :: test;

myarray1=[array1('a',0,0)]

     done = .FALSE.
     call random_number(rnd1);
     ra = nint(rnd1*size(myarray));  

       print*, "marray is ", myarray;
       print*, "myarray(ra) is ", myarray(1);
       print*, "myarray1 is  ", myarray1;

       myarray1 = myarray(1);     !<------ source of error.

        print*, "the new myarray1 is  ", myarray1;

 start1=1;
 out11=start1;
 test=1;
 list1=test+list2;

 end function function2

end function function1

program test1

use iso_fortran_env
implicit none

integer                 :: start, xout;
integer                 :: function1;

start = 1;
xout=function1(2);
print*, "my output is ", xout;

end program test1 

myarray1的值应具有myarray中的任何值(3之1)上下文。 我如何正确地写这行“ myarray1 = myarray(1)”。

0 个答案:

没有答案