Chapter 12 — Teaching Notes

One-line purpose. Dismantle the "Fortran can't do text" belief by teaching the three tools that make modern Fortran string handling clean — deferred-length strings, the character intrinsics, and internal files — and put them to work naming the solver's output files.

Key ideas to emphasize

  • Deferred-length = allocatable, for characters. This is the load-bearing idea. If students already understand allocatable arrays (Ch. 5) and the allocatable-over-pointer rule (Ch. 11), then character(:), allocatable needs almost no new machinery — it owns its memory, sizes itself from assignment, and reallocates on the next one. Frame the whole chapter as "the allocatable idea, applied to text," and it lands fast.
  • scan and verify are duals. Students reliably confuse them. Drill the one-liner: scan finds a character that IS in the set; verify finds the first that is NOT. Then show the payoff — verify(s, '0123456789') == 0 means "all digits," and the tokenizer uses verify to find a token's start and scan its end. Once they see them as complementary boundary-finders, the tokenizer is obvious.
  • Internal files reuse Chapter 7, they are not a new sublanguage. The single most reassuring point: write(str, fmt) is the same statement and same edit descriptors as writing to a file — the unit is just a string. Contrast with C's separate printf format language and Python's own format mini-language. This is why the chapter can lean entirely on prior knowledge for number↔text.
  • The trailing-blank trap is the #1 real bug. dir // '/x' with a fixed-length dir splicing in blanks is the mistake every student makes once. Show it, name the fix (trim), and connect it to why deferred-length strings don't have the problem — they carry no padding.
  • i6.6 is the whole Project Checkpoint. Everything about the filename heat_000123.vtk is in that one descriptor: width 6, zero-padded to 6 digits, so frames sort in time order. Spend a minute on why zero-padding matters (lexical sort = numeric sort) — it is a genuinely useful, non-obvious point.

Misconceptions to preempt

  • "Fortran strings are fixed-length / painful." (That is FORTRAN 77; character(:), allocatable is dynamic and pleasant.)
  • "index returns true/false." (It returns a position, 0 when absent; if (index(...)) won't even compile — a good teachable moment about Fortran's type checking.)
  • "index matches Python's find." (1-based vs 0-based, and 0 vs −1 when absent — two differences at once; a naive port is wrong twice.)
  • "trim removes all blanks." (Trailing only; trim(adjustl(s)) for both ends; it never touches interior blanks.)
  • "A too-big number in a narrow field truncates." (No — it fills with *; Fortran refuses to silently corrupt.)
  • "adjustl shortens the string." (No — length is preserved; blanks are moved to the other end.)

A live demonstration (5–8 minutes)

Type the tokenizer from §12.5 live, then run it on 'the quick brown' (with a deliberate double space). Ask the class to predict the number of tokens before running — many will say 4, expecting an empty token from the double space. Reveal 3, then change the input to add leading/trailing spaces and a comma-set delimiter (' ,') to show it still works. The "why no empty token?" moment (because verify skips a whole run of blanks) is the concept the whole section is built to deliver. Follow with a one-line internal write, write(buf,'(a,i6.6,a)') 'heat_', 42, '.vtk', and have them predict heat_000042.vtk.

Class-time budget (~50 min)

  • 8 min: fixed vs deferred length; the trailing-blank problem and how deferred-length dissolves it (§12.1).
  • 14 min: the character intrinsics, emphasizing scan/verify and the index return-value trap (§12.2), with the results table worked live.
  • 6 min: concatenation and substrings; the trim-before-// rule (§12.3).
  • 10 min: internal files — number↔text, i6.6 vs i0, the asterisk-overflow (§12.4).
  • 8 min: the tokenizer, live (§12.5).
  • 4 min: modern vs F77 (§12.6) + launch the Project Checkpoint filename helper.

Prerequisites to review

The character type and dp (Ch. 3); allocatable and automatic reallocation on assignment (Ch. 5); the edit descriptors I, F, A and list-directed I/O (Ch. 7); intent and assumed-shape arguments (Ch. 6). A two-minute recap of "what allocatable gave arrays" primes the whole deferred-length discussion.

Connections

Back: Ch. 5 / Ch. 11 (allocatable, the prefer-allocatable rule), Ch. 7 (I/O and edit descriptors), Ch. 3 (types). Forward: Ch. 13 (the iostat seam in CS-02 Phase 5 becomes defensive validation and error stop), Ch. 26 (the frame filenames feed the per-timestep VTK writer). This chapter is deliberately light on performance — text is rarely the hot path — which is itself worth saying so students calibrate where strings sit in a numerical code.