typestar

count, any, all, pack in Fortran

Four questions asked of one dive-site array through logical masks.

program pack_count
    implicit none
    integer :: depths(8) = [3, 41, 12, 55, 8, 47, 22, 61]

    ! count, any, and all ask questions of a logical mask.
    print *, "deep sites:    ", count(depths > 40)
    print *, "any shallow?   ", any(depths < 5)
    print *, "all divable?   ", all(depths < 70)
    ! pack keeps just the elements the mask selects.
    print *, "the deep ones: ", pack(depths, depths > 40)
end program pack_count

How it works

  1. count(depths > 40) tallies the deep sites without a loop.
  2. any and all reduce a mask to a single yes or no.
  3. pack(depths, mask) keeps just the selected elements, as a smaller array.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
404
Tokens
79
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 73 seconds.

Type this snippet

Step 3 of 3 in Whole-array thinking, step 12 of 27 in Language basics.

← Previous Next →