Im new with programming
According to this code i want to export my output to csv file. The problem is i prefer to export every 100 number in each row which have totally 10 row, but it export all numbers in one column. How can i fix it?
program Console5 implicit none integer:: i,j real rand real, dimension (100):: a CALL RANDOM_SEED open(unit=1,file='test.csv',status='unknown') do i=1,10 do j=1,100 call random_number(rand) a(j)=(rand*1) write(1,*) a(j) end do end do end program
For completeness, I can think of the following approaches for writing a 10x100 table:
(a) Write elements individually
program p1 integer :: i, j real :: a open(unit=1,file='test.csv',status='unknown') do i = 1,10 do j = 1,100 ! compute next element call random_number(a) ! write element (no linebreak) write(1,'(g14.7,x)',advance='no') a end do write(1,*) ! newline end do close(1) end program
(b) Write line by line
program p2 integer :: i real, dimension(100) :: a open(unit=1,file='test.csv',status='unknown') do i = 1,10 ! compute next line call random_number(a) ! write line write(1,'(100(g14.7,x))') a end do close(1) end program
(c) Write entire table at once
program p3 real, dimension (100,10):: a ! precompute full table call random_number(a) ! write table open(unit=1,file='test.csv',status='unknown') write(1,'(100(g14.7,x))') a close(1) end program
If the table is not too large, I'd prefer the last variant (c) for simplicity and performance reasons. It also mandates strict separation of computation and IO. However, the first program (a) is the most flexible in that it can handle variable numbers of elements per line, alternating output formats, etc.
Note that all variants require formatted output, and I used the format 'g14.7' just as an example.
The example above are 'space' separated nor comma separated. If you want comma I would use:
write(1,'(100(g14.7,:,","))') a or write(1,'(*(g14.7,:,","))') a
The : (colon) terminates the format if there are no more items, thus stopping a trailing comma being written.
can you comment on the asterisk in your second format? I remember from experimenting with it that it behaves like an infinite repeat count, but couldn't find it documented anywhere in the standard or in the Intel compiler documentation.
Kind regards, Ferdinand
NOTE 10.7 The effect of an unlimited-format-item is as if its enclosed list were preceded by a very large repeat count. There is no file positioning implied by unlimited-format-item reversion. This may be used to write what is commonly called a comma separated value record. For example, WRITE( 10, ’( "IARRAY =", *( I0, :, ","))’) IARRAY produces a single record with a header and a comma separated list of integer values.
On the subject of comma separated value it is better to define a separator character that can be set by the software or user. Comma is generally OK in the USA and UK but in many places it is used as a decimal separator and semi-colon ";" is often used instead. This can be the source of many problems if not considered properly.