2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
107
Task/CSV-data-manipulation/Fortran/csv-data-manipulation-1.f
Normal file
107
Task/CSV-data-manipulation/Fortran/csv-data-manipulation-1.f
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
program rowsum
|
||||
implicit none
|
||||
character(:), allocatable :: line, name, a(:)
|
||||
character(20) :: fmt
|
||||
double precision, allocatable :: v(:)
|
||||
integer :: n, nrow, ncol, i
|
||||
|
||||
call get_command_argument(1, length=n)
|
||||
allocate(character(n) :: name)
|
||||
call get_command_argument(1, name)
|
||||
open(unit=10, file=name, action="read", form="formatted", access="stream")
|
||||
deallocate(name)
|
||||
|
||||
call get_command_argument(2, length=n)
|
||||
allocate(character(n) :: name)
|
||||
call get_command_argument(2, name)
|
||||
open(unit=11, file=name, action="write", form="formatted", access="stream")
|
||||
deallocate(name)
|
||||
|
||||
nrow = 0
|
||||
ncol = 0
|
||||
do while (readline(10, line))
|
||||
nrow = nrow + 1
|
||||
|
||||
call split(line, a)
|
||||
|
||||
if (nrow == 1) then
|
||||
ncol = size(a)
|
||||
write(11, "(A)", advance="no") line
|
||||
write(11, "(A)") ",Sum"
|
||||
allocate(v(ncol + 1))
|
||||
write(fmt, "('(',G0,'(G0,:,''',A,'''))')") ncol + 1, ","
|
||||
else
|
||||
if (size(a) /= ncol) then
|
||||
print "(A,' ',G0)", "Invalid number of values on row", nrow
|
||||
stop
|
||||
end if
|
||||
|
||||
do i = 1, ncol
|
||||
read(a(i), *) v(i)
|
||||
end do
|
||||
v(ncol + 1) = sum(v(1:ncol))
|
||||
write(11, fmt) v
|
||||
end if
|
||||
end do
|
||||
close(10)
|
||||
close(11)
|
||||
contains
|
||||
function readline(unit, line)
|
||||
use iso_fortran_env
|
||||
logical :: readline
|
||||
integer :: unit, ios, n
|
||||
character(:), allocatable :: line
|
||||
character(10) :: buffer
|
||||
|
||||
line = ""
|
||||
readline = .false.
|
||||
do
|
||||
read(unit, "(A)", advance="no", size=n, iostat=ios) buffer
|
||||
if (ios == iostat_end) return
|
||||
readline = .true.
|
||||
line = line // buffer(1:n)
|
||||
if (ios == iostat_eor) return
|
||||
end do
|
||||
end function
|
||||
|
||||
subroutine split(line, array, separator)
|
||||
character(*) line
|
||||
character(:), allocatable :: array(:)
|
||||
character, optional :: separator
|
||||
character :: sep
|
||||
integer :: n, m, p, i, k
|
||||
|
||||
if (present(separator)) then
|
||||
sep = separator
|
||||
else
|
||||
sep = ","
|
||||
end if
|
||||
|
||||
n = len(line)
|
||||
m = 0
|
||||
p = 1
|
||||
k = 1
|
||||
do i = 1, n
|
||||
if (line(i:i) == sep) then
|
||||
p = p + 1
|
||||
m = max(m, i - k)
|
||||
k = i + 1
|
||||
end if
|
||||
end do
|
||||
m = max(m, n - k + 1)
|
||||
|
||||
if (allocated(array)) deallocate(array)
|
||||
allocate(character(m) :: array(p))
|
||||
|
||||
p = 1
|
||||
k = 1
|
||||
do i = 1, n
|
||||
if (line(i:i) == sep) then
|
||||
array(p) = line(k:i-1)
|
||||
p = p + 1
|
||||
k = i + 1
|
||||
end if
|
||||
end do
|
||||
array(p) = line(k:n)
|
||||
end subroutine
|
||||
end program
|
||||
20
Task/CSV-data-manipulation/Fortran/csv-data-manipulation-2.f
Normal file
20
Task/CSV-data-manipulation/Fortran/csv-data-manipulation-2.f
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
Copies a file with 5 comma-separated values to a line, appending a column holding their sum.
|
||||
INTEGER N !Instead of littering the source with "5"
|
||||
PARAMETER (N = 5) !Provide some provenance.
|
||||
CHARACTER*6 HEAD(N) !A perfect size?
|
||||
INTEGER X(N) !Integers suffice.
|
||||
INTEGER LINPR,IN !I/O unit numbers.
|
||||
LINPR = 6 !Standard output via this unit number.
|
||||
IN = 10 !Some unit number for the input file.
|
||||
OPEN (IN,FILE="CSVtest.csv",STATUS="OLD",ACTION="READ") !For formatted input.
|
||||
|
||||
READ (IN,*) HEAD !The first line has texts as column headings.
|
||||
WRITE (LINPR,1) (TRIM(HEAD(I)), I = 1,N),"Sum" !Append a "Sum" column.
|
||||
1 FORMAT (666(A:",")) !The : sez "stop if no list element awaits".
|
||||
2 READ (IN,*,END = 10) X !Read a line's worth of numbers, separated by commas or spaces.
|
||||
WRITE (LINPR,3) X,SUM(X) !Write, with a total appended.
|
||||
3 FORMAT (666(I0:",")) !I0 editing uses only as many columns as are needed.
|
||||
GO TO 2 !Do it again.
|
||||
|
||||
10 CLOSE (IN) !All done.
|
||||
END !That's all.
|
||||
Loading…
Add table
Add a link
Reference in a new issue