

NAMELIST provides an excellent way to add annotated input. Ignoring much of the difficulties of precise spacing and precision specifications needed for standard formatted READs. NAMELIST usually shines as a superior method for input, and is hardly used for output, except for checking what is acceptable NAMELIST input. It uses a syntax similar to keyword argument parsing. The order of the keywords is unimportant within the NAMELIST block. However, the order of NAMELIST blocks is important for READs. Some implementations will skip over NAMELIST blocks until the expected one is reached. If no REWIND is performed, you can have multiple NAMELIST blocks of the same name and differing data and read them in as needed.
Code examples
module datastuff use numeric_ implicit none ! for comparison purposes real :: ttreal, treal = 1.0 integer :: ttinteger, tinteger = 2 complex :: ttcomplex, tcomplex = (3.0, 4.0) character*10 :: ttchar, tchar = 'namelist' logical :: ttbool, tbool = .TRUE. real, dimension(4) :: aareal, & areal = (/ 1.0, 1.0, 2.0, 3.0 /) integer, dimension(4) :: aainteger, & ainteger = (/ 2, 2, 3, 4 /) complex, dimension(4) :: aacomplex, & acomplex = (/ (3.0, 4.0), & (3.0, 4.0), (5.0, 6.0), (7.0, 7.0) /) character*10, dimension(4) :: aachar, & achar = (/ 'namelist ', 'namelist ',& 'array ', ' the lot ' /) logical, dimension(4) :: aabool, & abool = (/ .TRUE., .TRUE., .FALSE., & .FALSE. /) real(kind=REAL_) :: xxreal, xreal = 1.0_REAL_ integer(kind=INT_) :: xxinteger, xinteger = 2_INT_ complex(kind=COMPLEX_) :: xxcomplex, & xcomplex = (3.0_COMPLEX_, 4.0_COMPLEX_) contains subroutine clearstuff() ttreal = 0.0 ttinteger = 0 ttcomplex = (0.0,0.0) ttchar = '' ttbool = .FALSE. aareal(1:4) = 0.0 aainteger(1:4) =0 aacomplex(1:4) =(0.0,0.0) aachar(1:4) = '' aabool(1:4) = .FALSE. xxreal = 0.0_REAL_ xxinteger = 0_INT_ xxcomplex = (0.0_COMPLEX_,0.0_COMPLEX_) end subroutine clearstuff subroutine diffstuff(fullout) implicit none logical :: fullout integer :: numbad,i ! Compare the input data to the expected value numbad = 0 if (ttreal .ne. treal) then numbad = numbad + 1 if (fullout) write(6,*) 'treal diff = ', ttreal - treal endif if (ttinteger .ne. tinteger) then numbad = numbad + 1 if (fullout) write(6,*) 'tinteger diff = ', ttinteger - tinteger endif if (ttcomplex .ne. tcomplex) then numbad = numbad + 1 if (fullout) write(6,*) 'tcomplex diff = ', ttcomplex - tcomplex endif if (ttchar .ne. tchar) then numbad = numbad + 1 if (fullout) write(6,*) 'tchar diff = ', ttchar, tchar endif if (ttbool .xor. tbool) then numbad = numbad + 1 if (fullout) write(6,*) 'tbool diff = ', ttbool, tbool endif do i = 1,4 if (aareal(i) .ne. areal(i)) then numbad = numbad + 1 if (fullout) write(6,*) 'areal diff = ', aareal(i) - areal(i) endif if (aainteger(i) .ne. ainteger(i)) then numbad = numbad + 1 if (fullout) write(6,*) 'ainteger diff = ', aainteger(i) - ainteger(i) endif if (aacomplex(i) .ne. acomplex(i)) then numbad = numbad + 1 if (fullout) write(6,*) 'acomplex diff = ', aacomplex(i) - acomplex(i) endif if (aachar(i) .ne. achar(i)) then numbad = numbad + 1 if (fullout) write(6,*) 'achar diff = ', aachar(i), achar(i) endif if (aabool(i) .xor. abool(i)) then numbad = numbad + 1 if (fullout) write(6,*) 'abool diff = ', aabool(i), abool(i) endif enddo if (xxreal .ne. xreal) then numbad = numbad + 1 if (fullout) write(6,*) 'xreal diff = ', xxreal - xreal endif if (xxinteger .ne. xinteger) then numbad = numbad + 1 if (fullout) write(6,*) 'xinteger diff = ', xxinteger - xinteger endif if (xxcomplex .ne. xcomplex) then numbad = numbad + 1 if (fullout) write(6,*) 'xcomplex diff = ', xxcomplex - xcomplex endif if (numbad .ne. 0) then write(6,'(1x,"found ",i3," differences")') numbad else write(6,'(1x,"found "," no"," differences")') endif end subroutine diffstuff end module datastuff program tnmlist use datastuff implicit none ! ! demonstrate the f90 standard namelist ! ! Note that NAMELIST syntax is similar to COMMON BLOCKs namelist /tdata/ treal, tinteger, tcomplex, tchar, tbool namelist /adata/ areal, ainteger, acomplex, achar, abool namelist /xdata/ xreal, xinteger, xcomplex namelist /ttdata/ ttreal, ttinteger, ttcomplex, ttchar, ttbool namelist /aadata/ aareal, aainteger, aacomplex, aachar, aabool namelist /xxdata/ xxreal, xxinteger, xxcomplex ! the OPEN statement defines many of the ! NAMELIST characteristics ! need the delim, else some implementations will not surround ! character strings with delimiters ! recl limits the I/O to 80 character lines open(6, recl=80, delim='APOSTROPHE') open(8,file="tnmlist.in", status='OLD', recl=80, delim='APOSTROPHE') ! ! NAMELIST output varies with compilers ! how NAMELIST data displays on your system write(6,nml=tdata) write(6,nml=adata) write(6,nml=xdata) write(6,*) write(6,*) 'Read first batch' call clearstuff() read(8,nml=ttdata) read(8,nml=aadata) read(8,nml=xxdata) ! using the KEYWORD feature of routine ! calls adds clarity call diffstuff(fullout=.TRUE.) write(6,*) 'Read second batch' call clearstuff() read(8,nml=ttdata) read(8,nml=aadata) read(8,nml=xxdata) call diffstuff(fullout=.TRUE.) end program tnmlist
! can have blank lines and comments in the namelist input file ! place these comments between NAMELISTs ! ! not every compiler supports comments within the namelist ! in particular vastf90/g77 does not ! ! some will skip NAMELISTs not directly referenced in read !&BOGUS rko=1 / ! &TTDATA TTREAL = 1., TTINTEGER = 2, TTCOMPLEX = (3.,4.), TTCHAR = 'namelist', TTBOOL = T/ &AADATA AAREAL = 1. 1. 2. 3., AAINTEGER = 2 2 3 4, AACOMPLEX = (3.,4.) (3.,4.) (5.,6.) (7.,7.), AACHAR = 'namelist' 'namelist' 'array' ' the lot', AABOOL = T T F F/ &XXDATA XXREAL = 1., XXINTEGER = 2, XXCOMPLEX = (3.,4.)/ ! ! the following output was generated by the Cray f90 compiler ! the Cray compiler supports comments within the namelist, ! and with an "assign" option will skip unreferenced NAMELISTs ! &TTDATA TTREAL = 1., TTINTEGER = 2, TTCOMPLEX = (3.,4.), TTCHAR = 'namelist ', TTBOOL = T / &AADATA AAREAL = 2*1., 2., 3., AAINTEGER = 2*2, 3, 4, AACOMPLEX = 2*(3.,4.), (5.,6.), (7.,7.), AACHAR = 2*'namelist ', 'array ', ' the lot ', AABOOL = 2*T, 2*F / &XXDATA XXREAL = 1., XXINTEGER = 2, XXCOMPLEX = (3.,4.) /
x86 Linux - g77/vastf90 &TDATA TREAL = 1., TINTEGER = 2, TCOMPLEX = (3.,4.), TCHAR = 'namelist', TBOOL = T/ &ADATA AREAL = 1. 1. 2. 3., AINTEGER = 2 2 3 4, ACOMPLEX = (3.,4.) (3.,4.) (5.,6.) (7.,7.), ACHAR = 'namelist' 'namelist' 'array' ' the lot', ABOOL = T T F F/ &XDATA XREAL = 1., XINTEGER = 2, XCOMPLEX = (3.,4.)/ Read first batch found no differences Read second batch found no differences

