There seems to be a restriction that the result of an array-valued
function must be an explicit-shape array or a pointer. Given that
restriction, how would one write a function with behavior similar to
the F90 PACK intrinsic? In many functions, the shape of the result is
related to the shape(s) of the argument(s). The case of PACK is an
exception - the shape of the result cannot be declared as a function
of the arguments.
Here is some code which is an attempt at a simple implementation of
PACK. It doesn't compile because the result is an assumed-shape
array. How do I modify it to make it FORTRAN?
thanks,
-robert
======================================================================
module mypack_module
contains
function mypack(ARRAY, MASK)
implicit none
integer, dimension(:) :: ARRAY
logical, dimension(:) :: MASK
integer, dimension(:) :: mypack
! Local variables
integer :: i, j
j = 1
do i = 1, SIZE(ARRAY)
if (MASK(i)) then
mypack(j) = ARRAY(i)
j = j+1
end if
end do
return
end function mypack
end module mypack_module
program test_mypack
use mypack_module
integer, dimension(3) :: A
integer, dimension(2) :: B
logical, dimension(3) :: Mask
A = (/ 1, 2, 3 /)
Mask = A <= 2
B = mypack(A, Mask)
stop
end program test_mypack
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|