typestar

Array sections in Fortran

start:end slices of a week of highs, with strides, ends, and in-place assignment.

program array_slices
    implicit none
    integer :: week(7) = [61, 68, 74, 79, 72, 65, 58]

    ! A section is start:end, with an optional stride.
    print *, "midweek:   ", week(3:5)
    print *, "weekend:   ", week(6:)
    print *, "every 2nd: ", week(1:7:2)
    ! Sections are arrays too: assign to one in place.
    week(1:2) = week(1:2) + 1
    print *, "warmed:    ", week(1:2)
end program array_slices

How it works

  1. week(3:5) takes midweek; week(6:) runs to the end.
  2. A third number is the stride: week(1:7:2) takes every other day.
  3. Sections are arrays: week(1:2) = week(1:2) + 1 updates in place.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
375
Tokens
91
Three-star pace
65 tpm

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

Type this snippet

Step 2 of 3 in Arrays, step 5 of 27 in Language basics.

← Previous Next →