0

I'm trying to return a vector of strings in fortran 90 but it's not working.

This is my main program and i want to fill in solutions with readFromCSV function.

program write_csv   
character(LEN=30) :: fileName;
character(LEN=200) :: head1,head2,head3,head4,head5
character(len=50), dimension(10), external :: readFromCSV
character(len=50), dimension(10) :: solutions

fileName = "solutions.csv";
solutions = readFromCSV(fileName)
end program

ReadFromCSC function code:

function readFromCSV(fileName) result (solutions)
integer :: res
character(len=50), dimension(10) :: solutions
character(LEN=10) :: aux, aux2
character(LEN=30) :: fileName
open(10, file=fileName,access='sequential',form="formatted",iostat=res)

i = 1;
DO WHILE (i < 11)
    read(10,fmt='(A)', iostat=res) solutions(i)
    if(i < 10) THEN
        solutions(i) = TRIM(solutions(i))
        solutions(i) = solutions(i)(19:(LEN(solutions(i))))
    ELSE
        solutions(i) = TRIM(solutions(i))
        solutions(i) = solutions(i)(20:(LEN(solutions(i))))
    END IF

        aux = solutions(i)(1:7)
        aux2 = solutions(i)(9:LEN(solutions(i)))
        solutions(i) = TRIM(aux)//TRIM(aux2)

    i = i+1
end do
i = 1;
DO WHILE (i < 11)
    write(*,'(a)') (solutions(i))
    i = i+1
end do
readFromCSV = solutions
end function readFromCSV
Axel Ros
  • 113
  • 5
  • Please show what makes you think (probably correctly) that it isn't working. Compiler errors, wrong results? – francescalus Jan 25 '18 at 19:10
  • Crucially, though, search here for "explicit interface". As explained [here](https://stackoverflow.com/a/31630662/3157076) you need such a thing in this case. Many answers will tell you how to make that happen. – francescalus Jan 25 '18 at 19:11
  • @francescalus the first error that it shows when i compile is that the external atribute generates a conflict with dimension atribute. But if i remove it, it says that readfromcsv is not a variable and the index of the matrix in (1) must be type INTEGER, and it found CHARACTER. – Axel Ros Jan 25 '18 at 19:14
  • You can't declare the function result in that way, but must use an interface block or modules (better) or an internal program. I can't find a good post for you immediately, however. Those are search terms for you, though. – francescalus Jan 25 '18 at 19:23
  • https://stackoverflow.com/a/16492376/1004168 – agentp Jan 25 '18 at 20:16
  • Alternatively, you might want to use a library that will read the csv file for you, have a look at https://github.com/jacobwilliams/fortran-csv-module – Pierre de Buyl Jan 25 '18 at 21:00
  • I disagree that this is a duplicate. And these suggestions are to difficult for such a simple problem. Just turn it into a subroutine instead of a function. `subroutine readFromCSV(fileName,solutions)` – Dan Sp. Jan 26 '18 at 15:47

0 Answers0