I do most of my programming in C/C++ and Perl, but I currently learning Fortran. I began by coding up a simple program, something that took me < 5 minutes in Perl (see this thread from BioStar). After working for several hours on a solution in Fortran 95 (teaching myself as I go), I came to this solution.
! Variable definitions integer :: argc integer :: num_digits integer :: i, j integer :: iocode integer :: line_length integer :: value character ( len=256 ) :: infile character ( len=16 ) :: int_format character ( len=16 ) :: int_string character ( len=2048 ) :: line ! Verify and parse command line arguments argc = iargc() if( argc < 1 ) then write(*, '(A, I0, A)') "Error: please provide file name (argc=", argc, ")" stop endif call getarg(1, infile) ! Open input file, croak if there is an issue open( unit=1, file=infile, action='read', iostat=iocode ) if( iocode > 0 ) then write(*, '(A, A, A)') "Error: unable to open input file '", trim(infile), "'" stop endif ! Process the file, print in CSV format do while(1 == 1) ! Read the line, skip if it is empty 100 read( unit=1, fmt='(A)', end=200 ) line line_length = len_trim(line) if( line_length == 0 ) then goto 100 endif ! The first value in the line is a string ! Find string boundaries and print it out i = 0 do while( line(i:i) == ' ' ) i = i+1 end do j = i do while( line(j:j) /= ' ' ) j = j+1 end do write(*, '(A)', advance="no") line(i:j-1) ! Now grab the rest of the integer values ! on the line, multiply them by 3, and print i = j j = 0 do while( i < line_length) do while( line(i:i) == ' ' ) i = i+1 end do j = i do while( j < line_length .and. line(j:j) /= ' ' ) j = j+1 end do int_string = line(i:j-1) read(int_string, '(I)') value value = value*3 write(*, '(A, I0)', advance="no") ",", value i = j j = 0 end do print * end do 200 close( 1 ) end program
There is a lot that I don't like about how I've written this program, but it's hard to separate my inexperience with the new syntax from bad practice. One thing in particular that I don't like is my use of labels and the infamous
goto statement. I'm interested in any feedback whatsoever, but I'm particularly interested in better ways to handle the control structure of the program (without using the
goto statement and
end parameter in the
read function if possible).
The compilers I have access to only support features through f95.