021. pack selects elements#

topic: Arrays

pack.f90 | | Godbolt Compiler Explorer logo | Fortran logo#
integer :: v(3) = [1, -4, 9]

print *, pack(v, v > 0)  ! 1 9 -- keep values > 0
print *, count(v > 0)    ! 2   -- count values > 0
print *, size(v)         ! 3
print *, sum(v)          ! 6
print *, sum(v, v > 0)   ! 10  -- `sum` has optional mask
print *, sum(pack(v, v > 0))  ! 10

end
Output1#
           1           9
           2
           3
           6
          10
          10


1

Compiled using GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 with no flags