I am writing a code where I need to arrays defined as u1,u2,u3. I require that number of variables defined are dictated by the user. for example if the user enters an integer value of "7". Then the variables defined are u1,u2,u3,u4,u5,u6,u7. So the variable names for the arrays are being defined by what value the user enters.
Asked
Active
Viewed 730 times
0
-
possible duplicate of [FORTRAN – dynamic variable names](http://stackoverflow.com/questions/9536346/fortran-dynamic-variable-names) – francescalus Jan 29 '15 at 23:20
-
@francescalus I dont understand the answer to the question which is similar to this and I am not allowed to comment there because of less reputation. – Syed Moez Jan 29 '15 at 23:54
-
I am also not able to open this link provided there: http://web.mse.uiuc.edu/courses/mse485/comp_info/derived.html – Syed Moez Jan 29 '15 at 23:54
1 Answers
1
From the description of your problem, you simply want an allocatable array.
TYPE(whatever), ALLOCATABLE :: u(:)
INTEGER :: some_number
PRINT *, 'Enter the number of things you want:'
READ *, some_number
ALLOCATE(u(some_number))
! work with u(1) through to u(some_number)
Standard Fortran does not offer dynamic variable naming "out of the box".

IanH
- 21,026
- 2
- 37
- 59
-
what I want to have is number of arrays depending on the user input. e.g. u1(10), u2(10), u3(10). however, the number of arrays that get defined depends on the user input. if the user inputs i=3, then it defines three arrays.... – Syed Moez Jan 30 '15 at 01:06
-
@SyedMoez use a 2 dimensional allocatable array. If you need 3 arrays of 10 values, delcare as `allocatable, u(:,:)` and allocate it with `allocate(u(3,10))`. – casey Jan 30 '15 at 01:11
-
1
-
If it is better to allocate as `u(10,3)` instead of `u(3,10)` if you want to pass subarrays of 10 elements to subroutines or functions, because the 10 elements will be contiguous in memory. – Anthony Scemama Jan 30 '15 at 07:44