FORTRAN 77 Language Reference

NAMELIST I/O

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.

Syntax Rules

The syntax of the NAMELIST statement is:

NAMELIST /group-name/namelist[[,]/group-name/namelist]...

Parameter 

Description 

group-name

Name of group  

namelist

List of variables or arrays, separated by commas 

See "NAMELIST" for details.

Example: NAMELIST statement:


	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.

Restrictions

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.

Example: A variable in two NAMELIST groups:


	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.

Output Actions

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.

The syntax of NAMELIST WRITE is:


WRITE ( extu,  namelist-specifier
 [, iostat] 
[, err]) 

where namelist-specifier has the form:


[NML=]group-name 

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.

Example: NAMELIST output:


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.

The WRITE can have the form of the following example:


	WRITE ( UNIT=6, NML=CASE ) 

Input Actions

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.

The syntax of NAMELIST READ is:


READ ( extu, namelist-specifier [, iostat] [, err] [, end]) 

where namelist-specifier has the form:


[NML=]group-name 

and group-name has been previously defined in a NAMELIST statement.

Example: NAMELIST input:


	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.

The READ can have the form of the following example:


	READ ( UNIT=1, NML=CASE ) 

Data Syntax

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.

The input data assignment statements must be in one of the following forms:

variable=value
array=value1[, value2,]...
array(subscript)=value1[, value2,]...
array(subscript,subscript)=value1[, value2,]...
variable=character constant
variable(index:index)=character constant

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.

The following is sample data to be read by the program segment above:


¤$case delta=0.05, mat( 2, 2 ) = 2.2, sample='Demo' $

The data could be on several records. Here NEW was not input, and the order is not the same as in the example NAMELIST statement:


¤$case 
¤delta=0.05 
¤mat( 2, 2 ) = 2.2 
¤sample='Demo'
¤$

Syntax Rules

The following syntax rules apply for input data to be read by NAMELIST:

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.

Example: NAMELIST input with some null data:


* nam2.f Namelist input with consecutive commas 
	REAL ARRAY(4,4) 
	NAMELIST /GRID/ ARRAY 
	WRITE ( *, * ) 'Input?' 
	READ ( *, GRID ) 
	WRITE ( *, GRID ) 
	END 

The data for nam2.f is:


¤$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.

Arrays Only

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.

Example: NAMELIST with repeat-factor in data:


* nam3.f Namelist "r*c" and "r* " 	REAL PSI(10) 	NAMELIST /GRID/ PSI 	WRITE ( *, * ) 'Input?' 	READ ( *, GRID ) 	WRITE ( *, GRID ) 	END 

The input for nam3.f is:


¤$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.

Example: NAMELIST input with some skipped data.

The other input is:


¤$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.

Name Requests

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.

Example: Requesting names:


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%