1
$\begingroup$

I am in the process of converting a FORTRAN 77 program to Wolfram Language. I have several COMMON BLOCK statements in FORTRAN 77. My question is how to deal with them in Wolfram Language?

My original approach is to declare all the variables on each COMMON BLOCK as global and them access them as needed on each subroutine. Does this is reasonable.

$\endgroup$
1
  • $\begingroup$ This is probably not an answer you are looking for, but I strongly suggest to look at "Big Ball of Mud": laputan.org/mud . I used the advice there fairly successfully to re-factor and convert a large F77 air-pollution simulation code base to C++ and Mathematica. $\endgroup$ Oct 5, 2015 at 17:06

1 Answer 1

6
$\begingroup$

If you want to have more than one "common block," one possibility would be to use a named Context to emulate each block. For example, here I define two "blocks," common1 and common2, and define a variable x in each of them. The Module below then uses these values by prefacing the desired x with the desired Context name:

common1`x = 1;

common2`x = 2;

f[x_] := Module[{x1 = common1`x, x2 = common2`x},
  {x, x1, x2}]

f[0]

(* ==> {0, 1, 2} *)

This shows that the symbol x has three different values inside the Module, and two of them are taken from the "globally" defined common blocks. Actually, normal "global" variables are in the Global context by default, and the use of named Contexts means that associated variables remain undefined in the Global context. So you also get

f[x]

(* ==> {x, 1, 2} *)

where the absence of a global value for x is apparent.

In each context, you can of course define multiple variables such as

common1`y

etc.

$\endgroup$
1
  • $\begingroup$ worth a mention with this construct if you change a local variable ( say x1 ) in the module you need to assign it back at the end (common1`x = x1 ). You could of course just use the verbose common1`x in the code. $\endgroup$
    – george2079
    Oct 5, 2015 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.