Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Format statement Question

pecan204
Beginner
621 Views

Is there a way to shorten a statement like this;

100 format('10=', F9.1,',',F9.1,',',F9.1,',',F9.1,',',F9.1,',',F9.1,',',F9.1,',')

I need to print out comma seperated values.Thearray is a variable size.I might need a few more floating format definitions depending on the array.

Thanks

0 Kudos
3 Replies
Jugoslav_Dujic
Valued Contributor II
621 Views
Format('10=', 1000(F9.1,','))

You can use any sufficiently big number -- the number of format descriptors need not match the number of I/O items (i.e. array size). If the former is greater, the excess is simply "discarded". (If the latter is greater, the format descriptors are "reused" according to some rules which I don't recall at the moment, but that shouldn't concern you anyway).

0 Kudos
anthonyrichards
New Contributor III
621 Views

..or you could try using variable formatting as demonstrated by this short program...

!****************************************************************************
!
! PROGRAM: format
!
! PURPOSE: Demonstrate variable formatting
!
!****************************************************************************
program format
implicit none
 integer :: outlu
character*8 form(5)
integer,parameter:: nsize=8
integer i
real*8 array(nsize)
! set up the format skeleton, with the second element left
! blank to receive the required integer multiple of the repeated
! format control (f9.1,',')
! This latter component is more than 8 characters long and so is
! spread over the third and fourth elements of variable FORM
! The fifth element of FORM just contains the closing bracket
! For a more detailed format, just add elements to array FORM as required
 data form/8h('10=', ,8h ,8H(f9.1,',,8H') ,8H) /
! set up some values for the array
do i=1,nsize
array(i)=real(i**2)
enddo

! Add the format multiplier using an internal write
! then show the complete format
write(form(2),'(i8)') nsize
 write(*,"('form=',5a8)") form
! Output the array to a file and then to the screen
outlu = 12
open (outlu,file='test.txt', status='unknown',form='formatted')

write(outlu,form) array
write(*,form) array
 close(outlu)
stop
end program format
0 Kudos
pecan204
Beginner
621 Views
Thanks, greatsuggestions!
0 Kudos
Reply