How to declare a variable mid-routine in Fortran

ryanjdillon

I would like to create an array with a dimension based on the number of elements meeting a certain condition in another array. This would require that I initialize an array mid-routine, which Fortran won't let me do.

Is there a way around that?

Example routine:

subroutine example(some_array)

real some_array(50) ! passed array of known dimension

element_count = 0
do i=1,50
  if (some_array.gt.0) then
    element_count = element_count+1
  endif
enddo

real new_array(element_count) ! new array with length based on conditional statement

endsubroutine example

Thanks!

francescalus

Your questions isn't about initializing an array, which involves setting its values.

However, there is a way to do what you want. You even have a choice, depending on how general it's to be.

I'm assuming that the element_count means to have a some_array(i) in that loop.

You can make new_array allocatable:

subroutine example(some_array)
  real some_array(50)
  real, allocatable :: new_array(:)

  allocate(new_array(COUNT(some_array.gt.0)))
end subroutine

Or have it as an automatic object:

subroutine example(some_array)
  real some_array(50)
  real new_array(COUNT(some_array.gt.0))
end subroutine

This latter works only when your condition is "simple". The allocatable case is much more general, such as when you want to use the full loop rather than the count intrinsic.

In both of these cases you meet the requirement of having all the declarations before executable statements.

[As a side-note, under Fortran 2008 the block construct allows automatic objects even after executable statements.]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to declare a variable mid-routine in Fortran

From Dev

Fortran final routine calls itself before variable goes out of scope

From Java

How can numpy be so much faster than my Fortran routine?

From Dev

How to call Fortran routine with unit number argument from C

From Dev

How to declare the type of a function that returns an array in Fortran?

From Dev

How to declare a complex type array in fortran 90

From Java

How to declare a variable in MySQL?

From Dev

how to declare a variable in Netezza?

From Dev

How to declare a variable in view

From Dev

How to declare a variable in scala

From Dev

Fortran/MPI: Is it possible to declare a variable on a single processor only?

From Java

How to declare a variable in a template in Angular

From Java

How to declare a global variable in React?

From Dev

How to declare string variable in assembly?

From Java

How to declare a local variable in Razor?

From Dev

How to declare a php variable to be constant

From Dev

How to declare a variable as !important with LESS?

From Dev

How to declare a "struct" variable in yacc

From Dev

How to declare variable with result of query?

From Dev

How to declare global variable in C?

From Dev

How to declare a variable for global use

From Dev

How to declare a variable inside an if condition?

From Dev

how to declare a variable to stored proc

From Dev

How to declare cursor as global variable

From Dev

How to declare string variable in assembly?

From Dev

How to declare a static variable in cuda

From Dev

How to declare a variable inside an if condition?

From Dev

How to declare a global variable in a module

From Dev

How to declare a variable using a different variable

Related Related

HotTag

Archive