[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP Fortran for OpenVMS
Language Reference Manual


Previous Contents Index

10.2.2.2 Implied-Do Lists in I/O Lists

In a data transfer statement, an implied-do list acts as though it were a part of an I/O statement within a DO loop. It takes the following form:


  • (list, do-var = expr1, expr2 [,expr3])

list

Is a list of variables, expressions, or constants (see Section 10.2.2.1).

do-var

Is the name of a scalar integer or real variable. The variable must not be one of the input items in list.

expr

Are scalar numeric expressions of type integer or real. They do not all have to be the same type, or the same type as the DO variable.

The implied-do loop is initiated, executed, and terminated in the same way as a DO construct.

The list is the range of the implied-do loop. Items in that list can refer to do-var, but they must not change the value of do-var.

Two nested implied-do lists must not have the same (or an associated) DO variable.

Use an implied-do list to do the following:

  • Specify iteration of part of an I/O list
  • Transfer part of an array
  • Transfer array items in a sequence different from the order of subscript progression

If the I/O statement containing an implied-do list terminates abnormally (with an END, EOR, or ERR branch or with an IOSTAT value other than zero), the DO variable becomes undefined.

Examples

The following two output statements are equivalent:


WRITE (3,200) (A,B,C, I=1,3)            ! An implied-do list

WRITE (3,200) A,B,C,A,B,C,A,B,C         ! A simple item list

The following example shows nested implied-do lists. Execution of the innermost list is repeated most often:


WRITE (6,150) ((FORM(K,L), L=1,10), K=1,10,2)

The inner DO loop is executed 10 times for each iteration of the outer loop; the second subscript (L) advances from 1 through 10 for each increment of the first subscript (K). This is the reverse of the normal array element order. Note that K is incremented by 2, so only the odd-numbered rows of the array are output.

In the following example, the entire list of the implied-do list (P(1), Q(1,1), Q(1,2)...,Q(1,10)) are read before I is incremented to 2:


READ (5,999) (P(I), (Q(I,J), J=1,10), I=1,5)

The following example uses fixed subscripts and subscripts that vary according to the implied-do list:


READ (3,5555) (BOX(1,J), J=1,10)

Input values are assigned to BOX(1,1) through BOX(1,10), but other elements of the array are not affected.

The following example shows how a DO variable can be output directly:


WRITE (6,1111) (I, I=1,20)

Integers 1 through 20 are written.

For More Information:

10.3 READ Statements

The READ statement is a data transfer input statement. Data can be input from external sequential, keyed-access or direct-access records, or from internal records.

10.3.1 Forms for Sequential READ Statements

Sequential READ statements transfer input data from external sequential-access records. The statements can be formatted with format specifiers (which can use list-directed formatting) or namelist specifiers (for namelist formatting), or they can be unformatted.

Sequential READ statements take one of the following forms:

Formatted


  • READ (eunit, format [,advance] [,size] [,iostat] [,err] [,end] [,eor]) [io-list]
  • READ form [,io-list]

Formatted: List-Directed


  • READ (eunit, * [,iostat] [,err] [,end]) [io-list]
  • READ * [,io-list]

Formatted: Namelist


  • READ (eunit, nml-group [,iostat] [,err] [,end])
  • READ nml

Unformatted


  • READ (eunit [,iostat] [,err] [,end]) [io-list]

eunit

Is an external unit specifier ([UNIT=]io-unit).

format

Is a format specifier ([FMT=]format).

advance

Is an advance specifier (ADVANCE=c-expr). If the value of c-expr is ' YES ' , the statement uses advancing input; if the value is ' NO ' , the statement uses nonadvancing input. The default value is ' YES ' .

size

Is a character count specifier (SIZE=i-var). It can only be specified for nonadvancing READ statements.

iostat

Is a status specifier (IOSTAT=i-var).

err, end, eor

Are branch specifiers if an error (ERR=label), end-of-file (END=label), or end-of-record (EOR=label) condition occurs.

EOR can only be specified for nonadvancing READ statements.

io-list

Is an I/O list.

form

Is the nonkeyword form of a format specifier (no FMT=).

*

Is the format specifier indicating list-directed formatting.

nml-group

Is a namelist specifier ([NML=]group) indicating namelist formatting.

nml

Is the nonkeyword form of a namelist specifier (no NML=) indicating namelist formatting.

For More Information:

  • On I/O control-list specifiers, see Section 10.2.1.
  • On I/O lists, see Section 10.2.2.
  • On advancing I/O, see Section 10.2.1.9 and the HP Fortran for OpenVMS User Manual.
  • On file sharing, see the HP Fortran for OpenVMS User Manual.

10.3.1.1 Rules for Formatted Sequential READ Statements

Formatted, sequential READ statements translate data from character to binary form by using format specifications for editing (if any). The translated data is assigned to the entities in the I/O list in the order in which the entities appear, from left to right.

Values can be transferred to objects of intrinsic or derived types. For derived types, values of intrinsic types are transferred to the components of intrinsic types that ultimately make up these structured objects.

For data transfer, the file must be positioned so that the record read is a formatted record or an end-of-file record.

If the number of I/O list items is less than the number of fields in an input record, the statement ignores the excess fields.

If the number of I/O list items is greater than the number of fields in an input record, the input record is padded with blanks. However, if PAD= ' NO ' was specified for file connection, the input list and file specification must not require more characters from the record than it contains. If more characters are required and nonadvancing input is in effect, an end-of-record condition occurs.

If the file is connected for unformatted I/O, formatted data transfer is prohibited.

Examples

The following example shows formatted, sequential READ statements:


READ (*, '(B)', ADVANCE='NO') C

READ (FMT="(E2.4)", UNIT=6, IOSTAT=IO_STATUS) A, B, C

10.3.1.2 Rules for List-Directed Sequential READ Statements

List-directed, sequential READ statements translate data from character to binary form by using the data types of the corresponding I/O list item to determine the form of the data. The translated data is then assigned to the entities in the I/O list in the order in which they appear, from left to right.

When a slash (/) is encountered during execution, the READ statement is terminated, and any remaining input list items are unchanged.

If the file is connected for unformatted I/O, list-directed data transfer is prohibited.

List-Directed Records

A list-directed external record consists of a sequence of values and value separators. A value can be any of the following:

  • A constant
    Each constant must be a literal constant of type integer, real, complex, logical, or character; or a nondelimited character string. Binary, octal, hexadecimal, Hollerith, and named constants are not permitted.
    In general, the form of the constant must be acceptable for the type of the list item. The data type of the constant determines the data type of the value and the translation from external to internal form. The following rules also apply:
    • A numeric list item can correspond only to a numeric constant, and a character list item can correspond only to a character constant. If the data types of a numeric list element and its corresponding numeric constant do not match, conversion is performed according to the rules for arithmetic assignment (see Table 4-2).
    • A complex constant has the form of a pair of real or integer constants separated by a comma and enclosed in parentheses. Blanks can appear between the opening parenthesis and the first constant, before and after the separating comma, and between the second constant and the closing parenthesis.
    • A logical constant represents true values (.TRUE. or any value beginning with T, .T, t, or .t) or false values (.FALSE. or any value beginning with F, .F, f, or .f).

    A character string does not need delimiting apostrophes or quotation marks if the corresponding I/O list item is of type default character, and the following is true:
    • The character string does not contain a blank, comma (,), or slash (/).
    • The character string is not continued across a record boundary.
    • The first nonblank character in the string is not an apostrophe or a quotation mark.
    • The leading character is not a string of digits followed by an asterisk.

    A nondelimited character string is terminated by the first blank, comma, slash, or end-of-record encountered. Apostrophes and quotation marks within nondelimited character strings are transferred as is.
  • A null value
    A null value is specified by two consecutive value separators (such as ,,) or a nonblank initial value separator. (A value separator before the end of the record does not signify a null value.)
    A null value indicates that the corresponding list element remains unchanged. A null value can represent an entire complex constant, but cannot be used for either part of a complex constant.
  • A repetition of a null value (r*) or a constant (r*constant), where r is an unsigned, nonzero, integer literal constant with no kind parameter, and no embedded blanks.

A value separator is any number of blanks, or a comma or slash, preceded or followed by any number of blanks. When any of these appear in a character constant, they are considered part of the constant, not value separators.

The end of a record is equivalent to a blank character, except when it occurs in a character constant. In this case, the end of the record is ignored, and the character constant is continued with the next record (the last character in the previous record is immediately followed by the first character of the next record).

Blanks at the beginning of a record are ignored unless they are part of a character constant continued from the previous record. In this case, the blanks at the beginning of the record are considered part of the constant.

Examples

Suppose the following statements are specified:


CHARACTER*14 C
DOUBLE PRECISION T
COMPLEX D,E
LOGICAL L,M
READ (1,*) I,R,D,E,L,M,J,K,S,T,C,A,B

Then suppose the following external record is read:


4 6.3 (3.4,4.2), (3, 2 ) , T,F,,3*14.6 ,'ABC,DEF/GHI''JK'/

The following values are assigned to the I/O list items:

I/O List Item Value Assigned
I 4
R 6.3
D (3.4,4.2)
E (3.0,2.0)
L .TRUE.
M .FALSE.
J Unchanged
K 14
S 14.6
T 14.6D0
C ABC,DEF/GHI ' JK
A Unchanged
B Unchanged

For More Information:

10.3.1.3 Rules for Namelist Sequential READ Statements

Namelist, sequential READ statements translate data from external to internal form by using the data types of the objects in the corresponding NAMELIST statement to determine the form of the data. The translated data is assigned to the specified objects in the namelist group in the order in which they appear, from left to right.

If a slash (/) is encountered during execution, the READ statement is terminated, and any remaining input list items are unchanged.

If the file is connected for unformatted I/O, namelist data transfer is prohibited.

Namelist Records

A namelist external record takes the following form:


  • &group-name object = value [,object = value].../

group-name

Is the name of the group containing the objects to be given values. The name must have been previously defined in a NAMELIST statement in the scoping unit. The name cannot contain embedded blanks and must be contained within a single record.

object

Is the name (or subobject designator) of an entity defined in the NAMELIST declaration of the group name. The object name must not contain embedded blanks except within the parentheses of a subscript or substring specifier. Each object must be contained in a single record.

value

Is any of the following:
  • A constant
    Each constant must be a literal constant of type integer, real, complex, logical, or character; or a nondelimited character string. Binary, octal, hexadecimal, Hollerith, and named constants are not permitted.
    In general, the form of the constant must be acceptable for the type of the list item. The data type of the constant determines the data type of the value and the translation from external to internal form. The following rules also apply:
    • A numeric list item can correspond only to a numeric constant, and a character list item can correspond only to a character constant. If the data types of a numeric list element and its corresponding numeric constant do not match, conversion is performed according to the rules for arithmetic assignment (see Table 4-2).
    • A complex constant has the form of a pair of real or integer constants separated by a comma and enclosed in parentheses. Blanks can appear between the opening parenthesis and the first constant, before and after the separating comma, and between the second constant and the closing parenthesis.
    • A logical constant represents true values (.TRUE. or any value beginning with T, .T, t, or .t) or false values (.FALSE. or any value beginning with F, .F, f, or .f).

    A character string does not need delimiting apostrophes or quotation marks if the corresponding NAMELIST item is of type default character, and the following is true:
    • The character string does not contain a blank, comma (,), slash (/), exclamation (!), ampersand (&), dollar sign ($), left parenthesis, equal sign (=), percent sign (%), or period (.).
    • The character string is not continued across a record boundary.
    • The first nonblank character in the string is not an apostrophe or a quotation mark.
    • The leading character is not a string of digits followed by an asterisk.

    A nondelimited character string is terminated by the first blank, comma, slash, end-of-record, exclamation, ampersand, or dollar sign encountered. Apostrophes and quotation marks within nondelimited character strings are transferred as is.
    If an equal sign, percent sign, or period is encountered while scanning for a nondelimited character string, the string is treated as a variable name (or part of one) and not as a nondelimited character string.
  • A null value
    A null value is specified by two consecutive value separators (such as ,,) or a nonblank initial value separator. (A value separator before the end of the record does not signify a null value.)
    A null value indicates that the corresponding list element remains unchanged. A null value can represent an entire complex constant, but cannot be used for either part of a complex constant.
  • A repetition of a null value (r*) or a constant (r*constant), where r is an unsigned, nonzero, integer literal constant with no kind parameter, and no embedded blanks.

Blanks can precede or follow the beginning ampersand (&), follow the group name, precede or follow the equal sign, or precede the terminating slash.

Comments (beginning with ! only) can appear anywhere in namelist input. The comment extends to the end of the source line.

If an entity appears more than once within the input record for a namelist data transfer, the last value is the one that is used.

If there is more than one object=value pair, they must be separated by value separators.

A value separator is any number of blanks, or a comma or slash, preceded or followed by any number of blanks. When any of these appear in a character constant, they are considered part of the constant, not value separators.

The end of a record is equivalent to a blank character, except when it occurs in a character constant. In this case, the end of the record is ignored, and the character constant is continued with the next record (the last character in the previous record is immediately followed by the first character of the next record).

Blanks at the beginning of a record are ignored unless they are part of a character constant continued from the previous record. In this case, the blanks at the beginning of the record are considered part of the constant.

Prompting for Namelist Group Information

During execution of a program containing a namelist READ statement, you can specify a question mark character (?) or a question mark character preceded by an equal sign (=?) to get information about the namelist group. The ? or =? must follow one or more blanks.

If specified for a unit capable of both input and output, the ? causes display of the group name and the objects in that group. The =? causes display of the group name, objects within that group, and the current values for those objects (in namelist output form). If specified for another type of unit, the symbols are ignored.

For example, consider the following statements:


NAMELIST /NLIST/ A,B,C
REAL A /1.5/
INTEGER B /2/
CHARACTER*5 C /'ABCDE'/

READ (5,NML=NLIST)
WRITE (6,NML=NLIST)
END

During execution, if a blank followed by ? is entered on a terminal device, the following values are displayed:


 &NLIST
   A
   B
   C
 /

If a blank followed by =? is entered, the following values are displayed:


 &NLIST
   A       =   1.500000    ,
   B       =           2,
   C       = ABCDE
 /

Examples

Suppose the following statements are specified:


NAMELIST /CONTROL/ TITLE, RESET, START, STOP, INTERVAL
CHARACTER*10 TITLE
REAL(KIND=8) START, STOP
LOGICAL(KIND=4) RESET
INTEGER(KIND=4) INTERVAL
READ (UNIT=1, NML=CONTROL)

The NAMELIST statement associates the group name CONTROL with a list of five objects. The corresponding READ statement reads the following input data from unit 1:


&CONTROL
   TITLE='TESTT002AA',
   INTERVAL=1,
   RESET=.TRUE.,
   START=10.2,
   STOP =14.5
/

The following values are assigned to objects in group CONTROL:

Namelist Object Value Assigned
TITLE TESTT002AA
RESET T
START 10.2
STOP 14.5
INTERVAL 1

It is not necessary to assign values to all of the objects declared in the corresponding NAMELIST group. If a namelist object does not appear in the input statement, its value (if any) is unchanged.

Similarly, when character substrings and array elements are specified, only the values of the specified variable substrings and array elements are changed. For example, suppose the following input is read:


&CONTROL TITLE(9:10)='BB' /

The new value for TITLE is TESTT002BB; only the last two characters in the variable change.

The following example shows an array as an object:


DIMENSION ARRAY_A(20)
NAMELIST /ELEM/ ARRAY_A
READ (UNIT=1,NML=ELEM)

Suppose the following input is read:


&ELEM
ARRAY_A=1.1, 1.2, , 1.4
/

The following values are assigned to the ARRAY_A elements:

Array Element Value Assigned
ARRAY_A(1) 1.1
ARRAY_A(2) 1.2
ARRAY_A(3) Unchanged
ARRAY_A(4) 1.4
ARRAY_A(5)...ARRAY(20) 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. For example, suppose the following input is read:


&ELEM
ARRAY_A(3)=34.54, 45.34, 87.63, 3*20.00
/

New values are assigned only to array ARRAY_A elements 3 through 8. The other element values are unchanged.

Nondelimited character strings that are written out by using a NAMELIST write may not be read in as expected by a corresponding NAMELIST read. Consider the following:


NAMELIST/TEST/ CHARR
CHARACTER*3 CHARR(4)
DATA CHARR/'AAA', 'BBB', 'CCC', 'DDD'/
OPEN (UNIT=1, FILE='NMLTEST.DAT')
WRITE (1, NML=TEST)
END

The output file NMLTEST.DAT will contain:


&TEST
CHARR   = AAABBBCCCDDD
/

If an attempt is then made to read the data in NMLTEST.DAT with a NAMELIST read using nondelimited character strings, as follows:


NAMELIST/TEST/ CHARR
CHARACTER*3 CHARR(4)
DATA CHARR/4*'   '/
OPEN (UNIT=1, FILE='NMLTEST.DAT')
READ (1, NML=TEST)
PRINT *, 'CHARR read in >', CHARR(1),'<  >',CHARR(2),'<  >',
1       CHARR(3), '<  >', CHARR(4), '<'
END

The result is the following:


CHARR read in >AAA<  <   >   <  >   <  >   <

For More Information:

  • On the NAMELIST statement, in general, and rules for objects in a namelist group, see Section 5.12.
  • On an alternative form for namelist external records, see Section B.10.
  • On namelist output, see Section 10.5.1.3.
  • On the general rules for formatted, sequential READ statements, see Section 10.3.1.1.


Previous Next Contents Index