Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

Simple List Items in I/O Lists

In a data transfer statement, a simple list of items takes the following form:

item [, item] ...

item

Is one of the following:

  • For input statements: a variable name

    The variable must not be an assumed-size array, unless one of the following appears in the last dimension: a subscript, a vector subscript, or a section subscript specifying an upper bound.

  • For output statements: a variable name, expression, or constant

    Any expression must not attempt further I/O operations on the same logical unit. For example, it must not refer to a function subprogram that performs I/O on the same logical unit.

The data transfer statement assigns values to (or transfers values from) the list items in the order in which the items appear, from left to right.

When multiple array names are used in the I/O list of an unformatted input or output statement, only one record is read or written, regardless of how many array name references appear in the list.

Examples

The following example shows a simple I/O list:

  WRITE (6,10) J, K(3), 4, (L+4)/2, N

When you use an array name reference in an I/O list, an input statement reads enough data to fill every item of the array. An output statement writes all of the values in the array.

Data transfer begins with the initial item of the array and proceeds in the order of subscript progression, with the leftmost subscript varying most rapidly. The following statement defines a two-dimensional array:

  DIMENSION ARRAY(3,3)

If the name ARRAY appears with no subscripts in a READ statement, that statement assigns values from the input record(s) to ARRAY(1,1), ARRAY(2,1), ARRAY(3,1), ARRAY(1,2), and so on through ARRAY(3,3).

An input record contains the following values:

  1,3,721.73

The following example shows how variables in the I/O list can be used in array subscripts later in the list:


  DIMENSION ARRAY(3,3)
  ...
  READ (1,30) J, K, ARRAY(J,K)

When the READ statement is executed, the first input value is assigned to J and the second to K, establishing the subscript values for ARRAY(J,K). The value 721.73 is then assigned to ARRAY(1,3). Note that the variables must appear before their use as array subscripts.

Consider the following derived-type definition and structure declaration:


  TYPE EMPLOYEE
    INTEGER ID
    CHARACTER(LEN=40) NAME
  END TYPE EMPLOYEE
  ...
  TYPE(EMPLOYEE) :: CONTRACT   ! A structure of type EMPLOYEE

The following statements are equivalent:


  READ *, CONTRACT

  READ *, CONTRACT%ID, CONTRACT%NAME

The following shows more examples:


 !  A variable and array element in iolist:
       REAL b(99)
       READ (*, 300) n, b(n) ! n and b(n) are the iolist
 300   FORMAT (I2, F10.5) ! FORMAT statement telling what form the input data has

 !  A derived type and type element in iolist:
       TYPE YOUR_DATA
         REAL a
         CHARACTER(30) info
         COMPLEX cx
       END TYPE YOUR_DATA
       TYPE (YOUR_DATA) yd1, yd2
       yd1%a = 2.3
       yd1%info = "This is a type demo."
       yd1%cx = (3.0, 4.0)
       yd2%cx = (4.5, 6.7)
 !  The iolist follows the WRITE (*,500).
       WRITE (*, 500) yd1, yd2%cx
 !  The format statement tells how the iolist will be output.
 500   FORMAT (F5.3, A21, F5.2, ',', F5.2, ' yd2%cx = (', F5.2,
        ',',F5.2, ' )')
 !  The output looks like:
 !  2.300This is a type demo 3.00, 4.00 yd2%cx = ( 4.50, 6.70 )

The following example uses an array and an array section:


 !  An array in the iolist:
       INTEGER handle(5)
       DATA handle / 5*0 /
       WRITE (*, 99) handle
 99    FORMAT (5I5)
 !  An array section in the iolist.
       WRITE (*, 100) handle(2:3)
 100   FORMAT (2I5)

The following shows another example:

 PRINT *,'(I5)', 2*3   ! The iolist is the expression 2*3. 

The following example uses namelist formatting:


 ! Namelist I/O:
      INTEGER int1
      LOGICAL log1
      REAL r1
      CHARACTER (20) char20
      NAMELIST /mylist/ int1, log1, r1, char20
      int1 = 1
      log1 = .TRUE.
      r1 = 1.0
      char20 = 'NAMELIST demo'
      OPEN (UNIT = 4, FILE = 'MYFILE.DAT', DELIM = 'APOSTROPHE')
      WRITE (UNIT = 4, NML = mylist)
 ! Writes the following:
 ! &MYLIST
 ! INT1 = 1,
 ! LOG1 = T,
 ! R1 = 1.000000,
 ! CHAR20 = 'NAMELIST demo '
 ! /
      REWIND(4)
      READ (4, mylist)

See Also