021. pack
selects elements#
topic: Arrays
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
1 9
2
3
6
10
10
Pack selects elements
— FortranTip (@fortrantip) December 19, 2021
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
- 1
Compiled using
GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
with no flags