Manual Reference Pages  - pack (3fortran)

NAME

PACK(3) - [ARRAY:CONSTRUCTION] Pack an array into an array of rank one

SYNOPSIS

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

         TYPE(kind=KIND) function pack(array,mask,vector)

TYPE(kind=KIND),option(in) :: array(..) logical :: mask(..) TYPE(kind=KIND),option(in),optional :: vector(*)

CHARACTERISTICS

o ARRAY is an array of any type
o MASK a logical scalar as well as an array conformable with ARRAY.
o VECTOR is of the same kind and type as ARRAY and of rank one
o the returned value is of the same kind and type as ARRAY

DESCRIPTION

PACK(3) 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, remaining positions are filled with elements taken from VECTOR

OPTIONS

o ARRAY : The data from this array is used to fill the resulting vector
o MASK : the logical mask must be the same size as ARRAY or, alternatively, it may be a logical scalar.
o VECTOR : 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.
VECTOR shall have at least as many elements as there are in ARRAY.

RESULT

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 [char46]true. values in MASK otherwise.

If MASK is scalar with the value .true., in which case the result size is the size of ARRAY.

EXAMPLES

Sample program:

    program demo_pack
    implicit none
    integer, allocatable :: m(:)
    character(len=10) :: c(4)

! gathering nonzero elements from an array: m = [ 1, 0, 0, 0, 5, 0 ] write(*, fmt="(*(i0, ’ ’))") pack(m, m /= 0)

! Gathering nonzero elements from an array and appending elements ! from VECTOR till the size of the mask array (or array size if the ! mask is scalar): m = [ 1, 0, 0, 2 ] write(*, fmt="(*(i0, ’ ’))") pack(m, m /= 0, [ 0, 0, 3, 4 ]) write(*, fmt="(*(i0, ’ ’))") pack(m, m /= 0 )

! select strings whose second character is "a" c = [ character(len=10) :: ’ape’, ’bat’, ’cat’, ’dog’] write(*, fmt="(*(g0, ’ ’))") pack(c, c(:)(2:2) == ’a’ )

end program demo_pack

Results:

     > 1 5
     > 1 2 3 4
     > 1 2
     > bat        cat

STANDARD

Fortran 95

SEE ALSO

MERGE(3), SPREAD(3), UNPACK(3)

fortran-lang intrinsic descriptions (license: MIT) @urbanjost


Nemo Release 3.1 pack (3fortran) April 28, 2024
Generated by manServer 1.08 from a94fd767-fdd6-4e37-a45a-6952f3ffddfd using man macros.