[UP]


Manual Reference Pages  - pack (3)

NAME

pack(3f) - [FORTRAN:INTRINSIC:ARRAY CONSTRUCTION] Pack an array into an array of rank one

CONTENTS

Syntax
Description
Arguments
Return Value
Example
Standard
Class
See Also

SYNTAX

result = pack(array, mask[,vector])

DESCRIPTION

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.

ARGUMENTS

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.

RETURN VALUE

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.

EXAMPLE

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

STANDARD

[[Fortran 95]] and later

CLASS

Transformational function

SEE ALSO

[[unpack]]


pack (3) March 18, 2019
Generated by manServer 1.08 from 6595ce80-e66d-4e54-8bd5-ba7c8c8c30af using man macros.