Taiseer
07-12-2011, 03:41 AM
Hi,
I want to have the format also varying. An example is:
do j=ny,1,-1
write(20,22) (array_colsXrows(i,j),i=1,nx)
enddo
22 format (640(e20.14,1x))
In this example I need the 640 to be a variable and I do not to change it each run. How can I do it?
Thanks
davekw7x
07-12-2011, 08:31 AM
...I want to have the format also varying. ...
One way is to create a format string using an internal file. I'll call it "fmt"
! Demo of varible format using an internal string
! By davekw7x
program VariableFormat
implicit none
integer num
integer istat
character(len=30) fmt
integer i
real farray(10)
do i = 1,10
farray(i) = i
enddo
write(*,'("How many per line: ")', advance = "no")
read(*,'(I10)',iostat=istat) num
if ((istat .ne. 0) .or. (num .le. 0)) then
write(*, '("Invalid entry. Must be a positive integer.")')
stop
end if
write(*,'("Writing ",I0," on each line")') num
write(fmt,'("(",I0,"(e20.14,1x))")') num
write(*, '("Here is the format for writing: ", a)') fmt
write(*,fmt) (farray(i), i = 1,10)
end program VariableFormat
Here's a run after compiling with GNU gfortran 4.1.2:
How many per line: 3
Writing 3 on each line
Here is the format for writing: (3(e20.14,1x))
0.10000000000000E+01 0.20000000000000E+01 0.30000000000000E+01
0.40000000000000E+01 0.50000000000000E+01 0.60000000000000E+01
0.70000000000000E+01 0.80000000000000E+01 0.90000000000000E+01
0.10000000000000E+02
Regards,
Dave