unwanted trailing commas when writing a .csv file


I have an output formatting problem.

I compile with the following lines

pgcc -c *.c
pgf77 -Mbounds -w -Bstatic *.f utilit.o -o flex.2012.1

My results array is called datacsv and is filled in correctly. I’m trying to write a .csv file to be read by a plotting program. The number of parameters fluctuates depending on user input to the program. Here is the format statement and the write statement:

  316 format(1p g20.10,99999(",",g20.10))

        do k = 1 , nslices
           write(32,316) (datacsv(listpar(i,j),k),j = 1 , npid(i))

The numbers appear in the file just fine separated by commas but a trailing comma appears in every line. The plotting program does not like that trailing comma. What would be the correct format to prevent the trailing commas?

Thanks for your time

Hi Dave,

The problem is the “99999”. When this format is being processed, the run time is expecting 99999 data items or until one too many items have been printed. It doesn’t know that it’s past the end until the “g20.10” is processed, in which case the extra “,” has already been printed. You’ll probably need to explicitly write out each element and use “ADVANCE=‘NO’” so a new line is not added. Something like:

% cat csv.f90 
      program test
  	integer :: j
        integer, parameter :: N=4
        real*8 :: datacsv(N)
        datacsv = 1.0
  316 format(1p g20.10,99999(",",g20.10))
       write(*,316) (datacsv(j),j = 1 , N)
  317 format(1p g20.10)
  318 format((g20.10,",")) 
       do j=1,N-1
         write(*,318,ADVANCE='NO') datacsv(j)
       write(*,317) datacsv(N)		  
       end program test
% pgf90 csv.f90
% a.out
     1.000000000    ,     1.000000000    ,     1.000000000    ,     1.000000000    ,
     1.000000000    ,     1.000000000    ,     1.000000000    ,     1.000000000

Hope this helps,


Never heard of that one.

So I had to rearrange some code and switch to pg90 because pg77 didn’t like the ADVANCE stuff.
Now I have

  317 format(1p g20.10                 )
  318 format((  g20.10,      ","       ))

        do k = 1 , nslices
           npidim1 = npid(i) - 1
           do j = 1 , npidim1
              write(32,318,ADVANCE='NO')  datacsv(listpar(i,     j ),k)
c             write(32,318             )  datacsv(listpar(i,     j ),k)
           write(32,317)                  datacsv(listpar(i,npid(i)),k)

and out comes

     0.000000000    ,    0.3000000000E-01,     0.000000000    
     25.00024414    ,     1.220000000    ,     1.000000000    
     50.00048828    ,     2.410000000    ,     3.000000000    
     75.00073242    ,     3.610000000    ,     4.000000000    
     100.0000000    ,     4.800000000    ,     5.000000000

It works. Now I don’t need to spend 10 minutes with the text editor after each run.
Thanks for your help.