Namelist-Directed Input/Output [ HP FORTRAN 77/iX Reference ] MPE/iX 5.0 Documentation
HP FORTRAN 77/iX Reference
Namelist-Directed Input/Output
Namelist-directed input/output statements are similar in function to
formatted statements. However, they use different mechanisms to control
the translation of data. Formatted I/O statements use explicit format
specifiers; namelist-directed I/O statements use data types.
Namelist Specifier
The namelist specifier is an argument used in an I/O statement to specify
that namelist-directed I/O is being used and to identify the group name
of the entities that may be modified on input or written on output.
Syntax
[NML =] group_name
-----------------------------------------------------------------------------------------------
| | | |
| Item | Description/Default | Restrictions |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| group_name | Name of a list of variables or | None. |
| | array names previously defined in | |
| | a NAMELIST statement. | |
| | | |
-----------------------------------------------------------------------------------------------
If the prefix NML= is omitted, the unit number must be the first item in
the list and the group name must be the second. A namelist specifier
cannot occur in a statement that contains a format specifier.
Namelist-Directed Input
The namelist-directed READ statement reads external records sequentially
until it finds the specified group_name. Using the data types of the
entities in the corresponding NAMELIST statement, it then translates the
data from external to internal form and assigns the translated data to
the specified namelist entities.
External File Syntax
$group_name entity1 = value1 [, entity2 = value2] [,...] $[END]
Example
NAMELIST /FOO/I,J,K,/BOO/L,M,K
CHARACTER*10 K
INTEGER I,J,L,M
:
OPEN(8,...)
READ(UNIT=8,NML=BOO)
A sample data file for FOO follows. Notice that the comma which follows
the string to initial K is optional.
$BOO L=10, M=45,
K='hellohello'
$
$FOO I=23
K='hello',
J=11
$END
Namelist-directed input data can contain a multiplier to repeat a value.
Input data can take either of the following forms:
r*c
r*
where r is an unsigned, nonzero integer constant and c is a constant.
The r*c form produces r repetitions of the constant c. The r* form
produces r repetitions of null values. Neither form can contain embedded
blanks, except where permitted in constant c.
The namelist-directed READ statement changes the values of only those
namelist entities that appear in the input data. Similarly, when
character substrings and array elements are specified, the values of only
the specified variable substrings and array elements are changed. When a
list of values is assigned to an array name, the first value in that list
is assigned to the first element of the array, the second value to the
second element, and so on. The consecutive commas indicate that the
value of the array element in that position remains unchanged.
When a list of values is assigned to an array element, the assignment
begins with the specified array element rather than with the first
element of the array.
You can input a question mark (?) to a namelist READ statement to print
the current value of all the items on the namelist. Input is allowed
from either a terminal or file and output is written to unit 6. Unit 6
must be connected to a terminal for the question mark to generate output.
If unit 6 is not connected to a terminal, the question mark is ignored.
The following is a sample namelist program and its output using the query
feature:
PROGRAM query
INTEGER lu, iu
PARAMETER (lu = 6)
PARAMETER (iu = 5)
CHARACTER*1 i1
CHARACTER*10 j1
CHARACTER*255 k1
DATA i1 /' '/
DATA j1 /' '/
NAMELIST/n/i1,j1,k1
DO i = 1,255
k1(i:i+1) = ' '
END DO
WRITE(lu,25) 'Enter ? to see current namelist values'
WRITE(lu,25) 'then enter the values i1 and j1 as shown below:'
WRITE(lu,25) '$N'
WRITE(lu,25) 'i1=''I'' j1=''CAN'''
WRITE(lu,25) '$END'
READ(iu,nml=n)
WRITE(lu,25) '-----------------------------------------------'
WRITE(lu,25) 'Enter ? to see i1 and j1 initialized'
WRITE(lu,25) 'then enter the value k1 as shown below:'
WRITE(lu,25) '$N'
WRITE(lu,25) 'k1=''ANSWER'''
WRITE(lu,25) '$END'
READ(iu,nml=n)
WRITE(lu,25) '-----------------------------------------------'
WRITE(lu,25) 'Enter ? to verify i1, j1 and k1 initialized'
WRITE(lu,25) 'then enter the following:'
WRITE(lu,25) '$N'
WRITE(lu,25) '$END'
READ(iu,nml=n)
WRITE(lu,25) '-----------------------------------------------'
WRITE(lu,25) 'Enter ? again to verify multiple question marks'
WRITE(lu,25) 'then enter the following:'
WRITE(lu,25) '$N'
WRITE(lu,25) '$END'
READ(iu,nml=n)
STOP
25 FORMAT(a)
END
The output is shown below. User input is underlined.
Enter ? to see current namelist values
then enter the values i1 and j1 as shown below:
$N
i1='I' j1='CAN'
$END
?
$N
I1 =' '
J1 =' '
K1 ='
'
$END
$N
i1='I' j1='CAN'
$END
-----------------------------------------------
Enter ? to see i1 and j1 initialized
then enter the value k1 as shown below:
$N
k1='ANSWER'
$END
?
$N
I1 ='I'
J1 ='CAN '
K1 ='
'
$END
$N
k1='ANSWER'
$END
-----------------------------------------------
Enter ? to verify i1, j1 and k1 initialized
then enter the following:
$N
$END
?
$N
I1 ='I'
J1 ='CAN '
K1 ='ANSWER
'
$END
$N
$END
-----------------------------------------------
Enter ? again to verify multiple question marks
then enter the following:
$N
$END
?
$N
I1 ='I'
J1 ='CAN '
K1 ='ANSWER
'
$END
$N
$END
Namelist-Directed Output
The namelist-directed WRITE statement is a sequential write that
translates internal storage to external records according to the
specified namelist. Only the brief forms of the relevant syntax elements
are shown here. For the complete syntax, refer to "READ Statement
(Executable)" .
Syntax
WRITE ( unit , [NML =] group_name [...] )
Namelist-directed output is suitable for use as namelist-directed input.
Namelist Output File Syntax
$group_name
entity = value
:
$END
Each entity is begun on a separate line.
Example
CHARACTER*5 BLA(2)
INTEGER HA,SOO
LOGICAL KOG
NAMELIST /BLANK/HA,SOO,KOG,BLA
BLA(1) = 'hello'
BLA(2) = 'HELLO'
KOG = .FALSE.
HA = 123
SOO = 17
:
WRITE(xxx,NML=BLANK)
:
END
Output:
$BLANK
HA = 123
SOO = 17
KOG = F
BLA = 'hello', 'HELLO'
$END
MPE/iX 5.0 Documentation