-1

I have a text file with two colums with real numbers.

How can I import the numbers from the second colum in an array ?

I think I first need to define an allocatble array, then read my text file and then allocate the second column to the array, but I don't know how to properly do it.

The text file looks like this:

1850     0.23

1851     0.56

1852     0.73

...

2000     0.98

(Two colums but I only need the second one (not the years))

My try:

program FINAL_PROJECT_A2 


      implicit none

real F_in_obs(0:150), funky

open(unit=10,file='Fin_obs.txt',status='old')

                  read(10,*) funky

                           do i=1,150

                                F_in_obs(i)=funky(2,i)

                           enddo

                  close(unit=10)

The error message is the following:

 F_in_obs(i)=funky(2,i)                  
                                1

Error: Unclassifiable statement at (1)

  • 2
    1) please edit your question to bring more information (code, etc) instead of posting comments; 2) just saying "it doesn't compile" is not enough, please copy/paste the full error messages from the compiler – PierU Dec 04 '22 at 12:20
  • I have edited my question ! It should be more clear now – Florian Hubart Dec 05 '22 at 13:17
  • 1
    Please use the code formatting features in the question editor. See my edit. Either the `{}` button, or the backticks \`. – Vladimir F Героям слава Dec 05 '22 at 13:23
  • 1
    You must also definitions of all your objects in your code. Use `implicit none` **always**. For example, `F_in_obs` must be somehow declared. – Vladimir F Героям слава Dec 05 '22 at 13:24
  • Don't worry the implicit none and variables were already declared in my code :)) – Florian Hubart Dec 05 '22 at 13:42
  • My final comment - your file has two numbers on each line, one integer and one real. Your code only reads one (real) number from each line. If you are not interested in the integer you still have to read it, but you don't have to store it. – High Performance Mark Dec 05 '22 at 13:45
  • It is not clear to me at all what `funky` is. The declaration is like for an ordinary scalar variable, but you are indexing it like a 2D array. You can just make it a 1D 2-element array `funky(2)` but then you have to read it in every loop iteration. – Vladimir F Героям слава Dec 05 '22 at 15:04
  • I would like to stress that we have **many** questions and answers about reading this kind of data already. Some links were already given to you. You should really read them and try to understand them. The understanding is the most important bit. Do not just blindly copy bits of code. – Vladimir F Героям слава Dec 05 '22 at 15:06
  • Florian, what are you intending to do with the data once you have read it? It may or may not be necessary to put it in an array at all. For example, you can calculate mean and variance just from rolling sums, and you can avoid the need to set up arrays to match the possibly-unknown number of data points. – lastchance Dec 05 '22 at 22:36

1 Answers1

1

I think that the following code with what you're looking for if you don't know how many lines the file has at the start. Assuming that you're dealing with Modern Fortran, which allows you to use allocatable arrays, then you don't need to know how long the file is at the beginning. You can figure out how many lines are in your file by running through it once.


    integer :: nlines = 0, io, i
    real, allocatable, dimension(:) :: array
    real :: skip
    open(1,file = 'test.txt')
    do
        read(1,*,iostat=io)
        if(io /= 0) exit
        nlines = nlines + 1
    end do

Then be sure to rewind your file with rewind(1) to start back at the end of the file. Then you can allocate the size of your data array like, allocate(array(nlines)). Then just loop through the array. In my example below, I just have the first column of data being written to a junk variable.

    do i = 1,nlines
        read(1,*) skip, array(i)
    end do

PaulPhy
  • 25
  • 7