The PACK function packs an array into a vector under the control of a mask.

## Syntax

PACK (array,mask [,vector])

*array* is an INTENT(IN) array can be of any type.

*mask* is INTENT(IN) and must be of type LOGICAL. *mask* must be conformable with *array*.

*vector* is an INTENT(IN) array of rank one, and must be the same type and kind as *array*. It must have at least as many elements as there are true elements in *array*. If *mask* is scalar with value true, *vector* must have at least as many elements as *array*.

The result is an array of rank one with the same type and kind as *array*.

If *vector* is present, the result size is the size of *vector*.

If *vector* is absent, the result size is the number of true elements in *mask* unless *mask* is scalar with the value true, in which case the size is the size of *array*.

The value of element i of the result is the ith true element of *mask*, in array-element order. If *vector* is present and is larger than the number of true elements in *mask*, the elements of the result beyond the number of true elements in *mask* are filled with values from the corresponding elements of *vector*.

## Example

integer :: c(3,3)=reshape((/0,3,2,4,3,2,5,1,2/),shape(c)) integer :: cc(9)=-1 write(*,'(3i3)') c ! writes 0 3 2 ! 4 3 2 ! 5 1 2 write(*,*) pack(c,mask=(c > 2)) ! writes 3 4 3 5 write(*,*) pack(c,mask=(c > 2),vector=cc) ! writes 3 4 3 5 -1 -1 -1 -1 -1 write(*,*) pack(c,.true.) ! writes 0 3 2 4 3 2 5 1 2