offeltoffel
offeltoffel

Reputation: 2801

Fortran: How to assign values in a module

I am struggling with a code I got from another person. He uses modules to supply arrays that are needed by the main routine. What I need, is an array with numbers from 500 to 3500.

He did that by writing

INTEGER :: i
REAL :: myArray(3001)

DATA (myArray(i),i=1,100)/&
500., 501., 502., 503., [...] 599./

DATA (myArray(i),i=101,200)/&
600., 601., [...], 699./

[...]

DATA (myArray(i),i=2901,3001)/&
[...] 3498., 3499., 3500./

Now to me this seems very complicated! Also, I need myArray to contain Integers, but of course I don't want to remove all those dots.

So at first I tried this:

Do i=1,3001
    myArray(i) = i+499
End Do

But I get

"error #6274: This statement must not appear in the specification part of a module"

What do I do wrong?

Upvotes: 2

Views: 1755

Answers (2)

John Alexiou
John Alexiou

Reputation: 29274

You need an implied do loop. This compiles and runs fine

MODULE module1

    INTEGER :: i
    REAL :: myArray(3001) = (/ (i, i=500, 3500) /)

END MODULE

The format is

(/ (exp1, var=start, end) /)

or

(/ (exp1, var=start, end, step) /)

or

(/ (exp1, expr2, .., var=start, end, step) /)

See https://web.stanford.edu/class/me200c/tutorial_90/07_arrays.html

Upvotes: 3

Coriolis
Coriolis

Reputation: 396

The error is quite explanatory. You can't perform calculations in a module. A module is designed to host variable declarations and subroutine interfaces. You have two options :

  • Just declare the type and the dimension of your array in the module and initialize it in your main program (using the module) with the loop you have suggested.

  • Or as pointed out by @francescalus, you can directly use an array constructor during the declaration in the module. Something like this : [(i, i=500, 3500)]

Upvotes: 1

Related Questions