Format reversion, repeat counts and scale factors

A format is not only a list of descriptors: it has a grammar. A count in front of a descriptor repeats it, a count in front of a group repeats the group, and when the output list holds more items than the format, the format starts again on a new record. Three more pieces complete the picture: the scale factor, the colon and the slash. This lesson uses each of them in a program of a few lines.

Repeat counts

3i5 is three integers in five columns each, and the count may sit in front of a group: 2(i3,a) is the group, printed twice. A repeat count of zero is legal and prints nothing, which is how a program builds a format with a computed number of columns.

The file repeat.f90:

program repeat_demo implicit none print '(a)', "repeat count:" print '(3i5)', 1, 2, 3 print '(a)', "nested repeat:" print '(2(i3,a))', 10, "x", 20, "y" print '(a)', "list longer than the format:" print '(2i4)', 1, 2, 3, 4, 5 end program repeat_demo

gfortran -Wall -Wextra -std=f2018 repeat.f90 -o repeat
./repeat

repeat count:
1 2 3
nested repeat:
10x 20y
list longer than the format:
1 2
3 4
5

The group prints 10x and 20y with no separator beyond the one in the format, and the last print shows the feature of the next section: five values through a two-value format come out as three records.

Format reversion

When the output list is longer than the format can consume, the format is used again: processing goes back to the last left parenthesis that still has a repeat count to use, or to the beginning, and a new record starts. The visible effect is a table with as many rows as the data needs, which is why a loop is often unnecessary:

print '(2i4)', 1, 2, 3, 4, 5 printed two values per line and the last line holds the odd one. A format '(2(i4))' reverts into the group rather than to the start, which matters when the format begins with a label that must not repeat on every row. That is the trick behind a repeating header: put the header inside the reverted group.

An unlimited repeat group *(...), new in Fortran 2008, says "as many times as the list needs" and is the modern way to write the same thing: print '(*(i3,","))', a prints a whole array through one format.

The scale factor

The P edit descriptor moves the decimal point of the following real descriptors and changes the exponent to compensate, so the value is the same and the digits shown are not. 1P gives one digit before the point, which is the form a scientific report usually wants.

program scale_demo implicit none print '(a)', "no scale factor:" print '(e12.4)', 1234.5 print '(a)', "1P scale factor:" print '(1pe12.4)', 1234.5 print '(a)', "0P and -1P:" print '(0pe12.4)', 1234.5 print '(-1pe12.4)', 1234.5 end program scale_demo

gfortran -Wall -Wextra -std=f2018 scale.f90 -o scale
./scale

no scale factor:
0.1234E+04
1P scale factor:
1.2345E+03
0P and -1P:
0.1234E+04
0.0123E+05

The exponent moved from four to three when the mantissa gained a digit, and -1P moves it the other way. The field is twelve characters in both lines, two of them blanks, which is the width the descriptor asks for.

The scale factor also applies to f in the other direction: a positive factor multiplies the value by ten to the power of the factor, so 2pf8.2 of 1.25 prints 125.00. That form is rare and confusing, and a program that needs it should say so in a comment.

The colon

A list shorter than its format leaves the remaining descriptors unused, and the literals between them are not printed either. The exception is a separator that must not appear after the last value: the colon descriptor stops format processing as soon as the list is exhausted, and writes nothing more.

program colon_demo implicit none integer :: a(4) character(len=80) :: buf a = [1, 2, 3, 4] write (buf, '(*(i3,:,","))') a print '(a)', "with a colon : [" // trim(buf) // "]" write (buf, '(*(i3,","))') a print '(a)', "without a colon : [" // trim(buf) // "]" end program colon_demo

gfortran -Wall -Wextra -std=f2018 colon.f90 -o colon
./colon

with a colon : [ 1, 2, 3, 4]
without a colon : [ 1, 2, 3, 4,]

The separator between the values is the comma of the format; without the colon the fourth value is followed by one more comma, which is the classic trailing-separator bug of a hand-written CSV. With the colon the format stops before it, and the string is the one a read expects.

The sibling of the colon is the slash: a / in a format ends the current record and starts a new one, which is how one write produces several lines. Both are about records rather than values, and both are worth knowing before a report of more than one line is written.

Exercises

  1. Print the numbers 1 to 7 through a format of 3i4. How many lines come out, and what is on the last one?
  2. Write the same format as an unlimited repeat group. Does the output change?
  3. Print 1234.5 with e12.4 and with 1pe12.4 and count the digits before the point in each.
  4. Remove the colon from colon.f90 and read the string with a list-directed read back into four integers. What happens?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does the repeat count in 3i5 do?

    Answer

    Prints three integers, each in five columns

    A count repeats the descriptor that follows it.

  2. What happens when the output list has more items than the format?

    Answer

    The format is used again and a new record starts

    Format reversion restarts the format for the remaining items.

  3. What does this program print? program p implicit none integer :: a(3) character(len=40) :: buf a = [1, 2, 3] write (buf, "(*(i3,:,1x))") a print "(a)", "[" // trim(buf) // "]" end program p

    Answer

    [ 1 2 3]

    The colon stops the format before the separator after the last value.

  4. What does the 1P scale factor do to e12.4?

    Answer

    Prints one digit before the decimal point and lowers the exponent by one

    The scale factor moves the point and compensates in the exponent.

  5. What is 0.1234E+04 with a 1P scale factor?

    Answer

    1.2345E+03

    One digit moves before the point and the exponent drops by one.

  6. What does the colon in a format do?

    Answer

    Stops format processing when the input or output list is exhausted

    The colon suppresses the rest of the format, including literals.

  7. Why does the format *(i3,:,",") avoid a trailing comma?

    Answer

    The colon stops the format before the comma after the last value

    The separator is only written while the list has items left.

  8. Which form repeats a group of descriptors twice?

    Answer

    2(i3,a)

    A count before a parenthesis repeats the whole group.

  9. What does a slash in a format do?

    Answer

    Ends the current record and starts a new one

    The slash is the record separator inside a format.

  10. What prints for the third value in print "(2i4)", 1, 2, 3, 4, 5?

    Answer

    A new record holding 3, 4 and then a record with 5

    Reversion gives records of two values, so the fifth value sits alone.

The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image