pack(3f) - [FORTRAN:INTRINSIC:ARRAY CONSTRUCTION] Pack an array into an array of rank one
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 implicit none call test1() call test2() call test3() contains ! subroutine test1() ! gathering nonzero elements from an array: integer :: m(6) m = [ 1, 0, 0, 0, 5, 0 ] write(*, fmt="(*(i0, ))") pack(m, m /= 0) ! "1 5" end subroutine test1 ! subroutine test2() ! Gathering nonzero elements from an array and appending elements ! from VECTOR: integer :: m(4) m = [ 1, 0, 0, 2 ] write(*, fmt="(*(i0, ))") pack(m, m /= 0, [ 0, 0, 3, 4 ]) ! "1 2 3 4" end subroutine test2 ! subroutine test3() ! select strings whose second character is "a" character(len=10) :: m(4) m = [ character(len=10) :: ape, bat, cat, dog] write(*, fmt="(*(g0, ))") pack(m, m(:)(2:2) == a ) ! "bat" "cat" end subroutine test3 ! end program demo_packResults:
1 5 1 2 3 4
bat cat
[[Fortran 95]] and later
Transformational function
unpack(3)
pack (3) | March 11, 2021 |