NAMELIST I/O produces format-free input or output of whole groups of variables, or input of selected items in a group of variables.@
The NAMELIST statement defines a group of variables or arrays. It specifies a group name, and lists the variables and arrays of that group.
The syntax of the NAMELIST statement is:
Name of group
List of variables or arrays, separated by commas
See "NAMELIST" for details.
CHARACTER*18 SAMPLE LOGICAL*4 NEW REAL*4 DELTA NAMELIST /CASE/ SAMPLE, NEW, DELTA
A variable or array can be listed in more than one NAMELIST group.
The input data can include array elements and strings. It can include substrings in the sense that the input constant data string can be shorter than the declared size of the variable.
group name can appear in only the NAMELIST, READ, or WRITE statements, and must be unique for the program.
list cannot include constants, array elements, dummy assumed-size arrays, structures, substrings, records, record fields, pointers, or pointer-based variables.
REAL ARRAY(4,4) CHARACTER*18 SAMPLE LOGICAL*4 NEW REAL*4 DELTA NAMELIST /CASE/ SAMPLE, NEW, DELTA NAMELIST /GRID/ ARRAY, DELTA
In the above example, DELTA is in the group CASE and in the group GRID.
NAMELIST output uses a special form of WRITE statement, which makes a report that shows the group name. For each variable of the group, it shows the name and current value in memory. It formats each value according to the type of each variable, and writes the report so that NAMELIST input can read it.
WRITE ( extu, namelist-specifier [, iostat] [, err])
and group-name has been previously defined in a NAMELIST statement.
The NAMELIST WRITE statement writes values of all variables in the group, in the same order as in the NAMELIST statement.
demo% cat nam1.f * nam1.f Namelist output CHARACTER*8 SAMPLE LOGICAL*4 NEW REAL*4 DELTA NAMELIST /CASE/ SAMPLE, NEW, DELTA DATA SAMPLE /'Demo'/, NEW /.TRUE./, DELTA /0.1/ WRITE ( *, CASE ) END demo% f77 nam1.f f77 nam1.f nam1.f: MAIN: demo% a.out D&case sample= Demo , new= T, delta= 0.100000 D&end demo%
Note that if you do omit the keyword NML then the unit parameter must be first, namelist-specifier must be second, and there must not be a format specifier.
WRITE ( UNIT=6, NML=CASE )
The NAMELIST input statement reads the next external record, skipping over column one, and looking for the symbol $ in column two or beyond, followed by the group name specified in the READ statement.
If the $group-name is not found, the input records are read until end of file.
The records are input and values assigned by matching names in the data with names in the group, using the data types of the variables in the group.
Variables in the group that are not found in the input data are unaltered.
READ ( extu, namelist-specifier [, iostat] [, err] [, end])
and group-name has been previously defined in a NAMELIST statement.
CHARACTER*14 SAMPLE LOGICAL*4 NEW REAL*4 DELTA, MAT(2,2) NAMELIST /CASE/ SAMPLE, NEW, DELTA, MAT READ ( 1, CASE )
In this example, the group CASE consists of the variables, SAMPLE, NEW, DELTA, and MAT. If you do omit the keyword NML, then you must also omit the keyword UNIT. The unit parameter must be first, namelist-specifier must be second, and there must not be a format specifier.
READ ( UNIT=1, NML=CASE )
The first record of NAMELIST input data has the special symbol $ (dollar sign) in column two or beyond, followed by the NAMELIST group name. This is followed by a series of assignment statements, starting in or after column two, on the same or subsequent records, each assigning a value to a variable (or one or more values to array elements) of the specified group. The input data is terminated with another $ in or after column two, as in the pattern:
¤$group-name variable =value [,variable =value,] $[END]
You can alternatively use an ampersand (&) in place of each dollar sign, but the beginning and ending delimiters must match. END is an optional part of the last delimiter.
If an array is subscripted, it must be subscripted with the appropriate number of subscripts: 1, 2, 3,...
Use quotes (either " or ') to delimit character constants. For more on character constants, see the next section.
¤$case delta=0.05, mat( 2, 2 ) = 2.2, sample='Demo' $
¤$case ¤delta=0.05 ¤mat( 2, 2 ) = 2.2 ¤sample='Demo' ¤$
The following syntax rules apply for input data to be read by NAMELIST:
The variables of the named group can be in any order, and any can be omitted.
The data must start in or after column two. Column one is totally ignored.
There must be at least one comma, space, or tab between variables, and one or more spaces or tabs are the same as a single space. Consecutive commas are not permitted before a variable name. Spaces before or after a comma have no effect.
No spaces or tabs are allowed inside a group name or a variable name, except around the commas of a subscript, around the colon of a substring, and after the ( and before the ) marks. No name can be split over two records.
The end of a record acts like a space character.
Note an exception--in a character constant, it is ignored, and the character constant is continued with the next record. The last character of the current record is immediately followed by the second character of the next record. The first character of each record is ignored.
The equal sign of the assignment statement can have zero or more blanks or tabs on each side of it.
Only constant values can be used for subscripts, range indicators of substrings, and the values assigned to variables or arrays. You cannot use a symbolic constant (parameter) in the actual input data.
Hollerith, octal, and hexadecimal constants are not permitted.
Each constant assigned has the same form as the corresponding FORTRAN constant.
There must be at least one comma, space, or tab between constants. Zero or more spaces or tabs are the same as a single space. You can enter: 1,2,3, or 1 2 3, or 1, 2, 3, and so forth.
Inside a character constant, consecutive spaces or tabs are preserved, not compressed.
A character constant is delimited by apostrophes (') or quotes ("), but if you start with one of those, you must finish that character constant with the same one. If you use the apostrophe as the delimiter, then to get an apostrophe in a string, use two consecutive apostrophes.
¤sample='use "$" in 2' (Read as: use $ in 2) ¤sample='don''t' (Read as: don't) ¤sample="don''t" (Read as: don''t) ¤sample="don't" (Read as: don't)
A complex constant is a pair of real or integer constants separated by a comma and enclosed in parentheses. Spaces can occur only around the punctuation.
A logical constant is any form of true or false value, such as .TRUE. or .FALSE., or any value beginning with .T, .F, and so on.
A null data item is denoted by two consecutive commas, and it means the corresponding array element or complex variable value is not to be changed. Null data item can be used with array elements or complex variables only. One null data item represents an entire complex constant; you cannot use it for either part of a complex constant.
* nam2.f Namelist input with consecutive commas REAL ARRAY(4,4) NAMELIST /GRID/ ARRAY WRITE ( *, * ) 'Input?' READ ( *, GRID ) WRITE ( *, GRID ) END
¤$GRID ARRAY = 9,9,9,9,,,,,8,8,8,8 $
This code loads 9s into row 1, skips 4 elements, and loads 8s into row 3 of ARRAY.
The forms r*c and r* can be used only with an array.
The form r*c stores r copies of the constant c into an array, where r is a nonzero, unsigned integer constant, and c is any constant.
* nam3.f Namelist "r*c" and "r* " REAL PSI(10) NAMELIST /GRID/ PSI WRITE ( *, * ) 'Input?' READ ( *, GRID ) WRITE ( *, GRID ) END
¤$GRID PSI = 5*980 $
The program, nam3.f, reads the above input and loads 980.0 into the first 5 elements of the array PSI.
The form r* skips r elements of an array (that is, does not change them), where r is an unsigned integer constant.
Example: NAMELIST input with some skipped data.
¤$GRID PSI = 3* 5*980 $
The program, nam3.f, with the above input, skips the first 3 elements and loads 980.0 into elements 4,5,6,7,8 of PSI.
If your program is doing NAMELIST input from the terminal, you can request the group name and NAMELIST names that it accepts.
To do so, enter a question mark (?) in column two and press Return. The group name and variable names are then displayed. The program then waits again for input.
demo% cat nam4.f * nam4.f Namelist: requesting names CHARACTER*14 SAMPLE LOGICAL*4 NEW REAL*4 DELTA NAMELIST /CASE/ SAMPLE, NEW, DELTA WRITE ( *, * ) 'Input?' READ ( *, CASE ) END demo% f77 -silent nam4.f demo% a.out Input? ¤? <-- Keyboard Input ¤$case ¤sample ¤new ¤delta ¤$end ¤$case sample="Test 2", delta=0.03 $ <-- Keyboard Input demo%