Search code examples
variablesfortranuser-defineddynamic-variables

fortran dynamic variables names


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.


Solution

  • 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".