pack(3f) - [FORTRAN:INTRINSIC:ARRAY CONSTRUCTION] Pack an array into an array of rank one
Syntax
Description
Arguments
Return Value
Example
Standard
Class
See Also
result = pack(array, mask[,vector])
Stores the elements of ARRAY in an array of rank one.
The beginning of the resulting array is made up of elements whose MASK equals TRUE. Afterwards, positions are filled with elements taken from VECTOR.
ARRAY - Shall be an array of any type. MASK - Shall be an array of type LOGICAL and of the same size as ARRAY. Alternatively, it may be a LOGICAL scalar. VECTOR - (Optional) shall be an array of the same type as ARRAY and of rank one. If present, the number of elements in VECTOR shall be equal to or greater than the number of true elements in MASK. If MASK is scalar, the number of elements in VECTOR shall be equal to or greater than the number of elements in ARRAY.
The result is an array of rank one and the same type as that of ARRAY. If VECTOR is present, the result size is that of VECTOR, the number of TRUE values in MASK otherwise.
Sample program:
program demo_pack ! Sample program gathering nonzero elements from an array: call test1() ! Gathering nonzero elements from an array and appending elements from VECTOR: call test2() contains ! subroutine test1() integer :: m(6) m = (/ 1, 0, 0, 0, 5, 0 /) write(*, fmt="(6(i0, ))") pack(m, m /= 0) ! "1 5" end subroutine test1 ! subroutine test2() integer :: m(4) m = (/ 1, 0, 0, 2 /) write(*, fmt="(4(i0, ))") pack(m, m /= 0, (/ 0, 0, 3, 4 /)) ! "1 2 3 4" end subroutine test2 ! end program demo_pack
[[Fortran 95]] and later
Transformational function
[[unpack]]
pack (3) | March 18, 2019 |