Beliavsky / FortranTip

Short instructional Fortran codes associated with Twitter @FortranTip
https://zmoon.github.io/FortranTipBrowser/
The Unlicense
64 stars 14 forks source link

partially reading a list using list-directed I/O #47

Closed urbanjost closed 2 years ago

urbanjost commented 2 years ago

List-directed input (using an asterisk("*") as a format) is often described simply as free-format input but it is more. It allows for null values and reading a partial list , and the form" r*c" where “r” is a repeater.

Suppose you have a program that prompts you for nine values to translate, scale, and/or rotate an object.

program listdirected
implicit none 
real :: x=0.0,y=0.0,z=0.0,  sx=1.0,sy=1.0,sz=1.0, rx=0.0,ry=0.0,rz=0.0
integer :: ios
character(len=256) :: msg, input
   do
      print *, 'Enter transformation value(s) or "stop"'
      print *, 'translate .........',x,y,z
      print *, 'scale .............',sx,sy,sz
      print *, 'rotate (degrees)...',rx,ry,rz
      read(*,'(a)',iostat=ios,iomsg=msg) input
      if(input.eq.'stop')exit
      read(input,*,iostat=ios,iomsg=msg) x,y,z,sx,sy,sz,rx,ry,rz
      if(ios.ne.0)then
         print *,"<ERROR>",trim(msg)
         cycle
      endif
      ! do something
   end do
end program listdirected

You do not have to enter all nine values. To just change all the scaling values to "2.0", you can enter:

  ,,,2,2,2/

Where the null values mean to skip changing that value, and the slash means the end of values, or equivalently

3*,3*2.0/

Where "3" skips three values, 32.0" sets the next three values to two, and the "/" means you are done supplying values.