common data in fortran77

This is a discussion on common data in fortran77 within the Fortran forums in Programming Languages category; I have 50 different integer variables for different purpose (so they are not stored in an integer array). After initialization, they will be used in many subroutines. Then, I need to declare all these 50 variables, and initialized them locally in each subroutine. It’s terrible to code in this way since it wastes many lines but do nothing different, and the code is not beautiful. Could somebody suggest any solutions? Thanks, Cheng’an...

Go Back   Application Development Forum > Programming Languages > Fortran

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-20-2008, 05:28 AM
tju329
Guest
 
Default common data in fortran77

I have 50 different integer variables for different purpose (so they
are not stored in an integer array). After initialization, they will
be used in many subroutines. Then, I need to declare all these 50
variables, and initialized them locally in each subroutine. It’s
terrible to code in this way since it wastes many lines but do nothing
different, and the code is not beautiful. Could somebody suggest any
solutions?

Thanks,
Cheng’an

Reply With Quote
  #2  
Old 08-20-2008, 05:45 AM
Arjen Markus
Guest
 
Default Re: common data in fortran77

On 20 aug, 11:28, tju329 <tju...@gmail.com> wrote:
> I have 50 different integer variables for different purpose (so they
> are not stored in an integer array). After initialization, they will
> be used in many subroutines. Then, I need to declare all these 50
> variables, and initialized them locally in each subroutine. It’s
> terrible to code in this way since it wastes many lines but do nothing
> different, and the code is not beautiful. Could somebody suggest any
> solutions?
>
> Thanks,
> Cheng’an


Are you stuck with FORTRAN 77 or can you use Fortran 90/95?

In the latter case, use a module that contains these variables
and use them in the various routines.

If you are stuck with FORTRAN 77 for a good and solid reason (*),
then COMMON blocks are most likely what you are looking for.
Put the declarations and the definition of the COMMON blocks
in a separate file so that you can use the INCLUDE statement
in each routine to, well, include these declarations and
definitions.

Regards,

Arjen

(*) There are several free Fortran 90/95 compilers available,
so not being able to buy one, is not really an excuse.
Reply With Quote
  #3  
Old 08-20-2008, 06:09 AM
David Flower
Guest
 
Default Re: common data in fortran77

On Aug 20, 10:28*am, tju329 <tju...@gmail.com> wrote:
> I have 50 different integer variables for different purpose (so they
> are not stored in an integer array). After initialization, they will
> be used in many subroutines. Then, I need to declare all these 50
> variables, and initialized them locally in each subroutine. It’s
> terrible to code in this way since it wastes many lines but do nothing
> different, and the code is not beautiful. Could somebody suggest any
> solutions?
>
> Thanks,
> Cheng’an


Note that you should initialise COMMON blocks in BLOCK DATA, and that
one of the commonest FORTRAN77 errors is to fail to link in BLOCK
DATA.

Incidentally, are you actually dealing with variables and not
constants ?

If the latter, and INCLUDE file containing appropriate type and
PARAMETER statements is the solution

Dave Flower
Reply With Quote
  #4  
Old 08-20-2008, 06:22 AM
tju329
Guest
 
Default Re: common data in fortran77

On Aug 20, 5:45*pm, Arjen Markus <arjen.mar...@wldelft.nl> wrote:
> On 20 aug, 11:28, tju329 <tju...@gmail.com> wrote:
>
> > I have 50 different integer variables for different purpose (so they
> > are not stored in an integer array). After initialization, they will
> > be used in many subroutines. Then, I need to declare all these 50
> > variables, and initialized them locally in each subroutine. It’s
> > terrible to code in this way since it wastes many lines but do nothing
> > different, and the code is not beautiful. Could somebody suggest any
> > solutions?

>
> > Thanks,
> > Cheng’an

>
> Are you stuck with FORTRAN 77 or can you use Fortran 90/95?
>
> In the latter case, use a module that contains these variables
> and use them in the various routines.
>
> If you are stuck with FORTRAN 77 for a good and solid reason (*),
> then COMMON blocks are most likely what you are looking for.
> Put the declarations and the definition of the COMMON blocks
> in a separate file so that you can use the INCLUDE statement
> in each routine to, well, include these declarations and
> definitions.
>
> Regards,
>
> Arjen
>
> (*) There are several free Fortran 90/95 compilers available,
> * * so not being able to buy one, is not really an excuse.


Hi Arjen,

Thanks for your reply and suggestions.

I have to use FORTRAN77. I forgot a very important thing in the last
post, i.e. I can not initialize these variables in simple way like
“length = 111”, instead I need to call a subroutine to get them out
from a “large database”. Common block can not work in my case. One
could say you can get them out once and reuse them repeatly later on,
but the problem is some of the variables could be changed by other
subroutines those are out of my control. Sync is necessary and should
be in the first step in my subroutines.

Thanks,

Cheng'an
Reply With Quote
  #5  
Old 08-20-2008, 06:37 AM
David Flower
Guest
 
Default Re: common data in fortran77

On Aug 20, 11:22*am, tju329 <tju...@gmail.com> wrote:
> On Aug 20, 5:45*pm, Arjen Markus <arjen.mar...@wldelft.nl> wrote:
>
>
>
>
>
> > On 20 aug, 11:28, tju329 <tju...@gmail.com> wrote:

>
> > > I have 50 different integer variables for different purpose (so they
> > > are not stored in an integer array). After initialization, they will
> > > be used in many subroutines. Then, I need to declare all these 50
> > > variables, and initialized them locally in each subroutine. It’s
> > > terrible to code in this way since it wastes many lines but do nothing
> > > different, and the code is not beautiful. Could somebody suggest any
> > > solutions?

>
> > > Thanks,
> > > Cheng’an

>
> > Are you stuck with FORTRAN 77 or can you use Fortran 90/95?

>
> > In the latter case, use a module that contains these variables
> > and use them in the various routines.

>
> > If you are stuck with FORTRAN 77 for a good and solid reason (*),
> > then COMMON blocks are most likely what you are looking for.
> > Put the declarations and the definition of the COMMON blocks
> > in a separate file so that you can use the INCLUDE statement
> > in each routine to, well, include these declarations and
> > definitions.

>
> > Regards,

>
> > Arjen

>
> > (*) There are several free Fortran 90/95 compilers available,
> > * * so not being able to buy one, is not really an excuse.

>
> Hi Arjen,
>
> Thanks for your reply and suggestions.
>
> I have to use FORTRAN77. I forgot a very important thing in the last
> post, i.e. I can not initialize these variables in simple way like
> “length = 111”, instead I need to call a subroutine to get them out
> from a “large database”. Common block can not work in my case. One
> could say you can get them out once and reuse them repeatly later on,
> but the problem is some of the variables could be changed by other
> subroutines those are out of my control. Sync is necessary and should
> be in the first step in my subroutines.
>
> Thanks,
>
> Cheng'an- Hide quoted text -
>
> - Show quoted text -


But if they are always the same variables, you just need a SUBROUTINE
to either:

a) Get them out of the database (again)

or

b) Restore the contents of the COMMON block to the status quo (in
which case you will still need (a), and a routine to store the data
somewhere

Dave Flower

Reply With Quote
  #6  
Old 08-20-2008, 04:00 PM
David Flower
Guest
 
Default Re: common data in fortran77

On Aug 20, 9:37*pm, glen herrmannsfeldt <g...@ugcs.caltech.edu> wrote:
> tju329 wrote:
> > I have 50 different integer variables for different purpose (so they
> > are not stored in an integer array). After initialization, they will
> > be used in many subroutines. Then, I need to declare all these 50
> > variables, and initialized them locally in each subroutine. It’s
> > terrible to code in this way since it wastes many lines but do nothing
> > different, and the code is not beautiful. Could somebody suggest any
> > solutions?

>
> I am not so sure what you are trying to do, but...
>
> If you have a large number of separate variables that you
> need to pass around to many routines, you could put them in
> a structure, though that requires features past Fortran 77.
>
> For Fortran 77, you can EQUIVALENCE them to array elements,
> and pass the array around. *They must be the same type, though.
> You can't EQUIVALENCE to dummy arguments, though.
>
> Also, you can initialize variables in a DATA statement.
> It isn't the same as an assignment statement, but DATA often
> works better for initializing many variables (or arrays).
>
> * * * *DATA I/1/,J/2/,K/3/,L/4/
> or
> * * * *DATA I,J,K,L/1,2,3,4/
>
> note that DATA gives variables the SAVE attribute,
> and that they are initialized only once.
>
> -- glen


No, the F77 standard is quite clear - if a local variable, initialised
by a DATA statement is assigned a different value, it becomed undefind
on exit from the route. (17.3.6.b)

Dave Flower
Reply With Quote
  #7  
Old 08-20-2008, 04:37 PM
glen herrmannsfeldt
Guest
 
Default Re: common data in fortran77

tju329 wrote:
> I have 50 different integer variables for different purpose (so they
> are not stored in an integer array). After initialization, they will
> be used in many subroutines. Then, I need to declare all these 50
> variables, and initialized them locally in each subroutine. It’s
> terrible to code in this way since it wastes many lines but do nothing
> different, and the code is not beautiful. Could somebody suggest any
> solutions?


I am not so sure what you are trying to do, but...

If you have a large number of separate variables that you
need to pass around to many routines, you could put them in
a structure, though that requires features past Fortran 77.

For Fortran 77, you can EQUIVALENCE them to array elements,
and pass the array around. They must be the same type, though.
You can't EQUIVALENCE to dummy arguments, though.


Also, you can initialize variables in a DATA statement.
It isn't the same as an assignment statement, but DATA often
works better for initializing many variables (or arrays).

DATA I/1/,J/2/,K/3/,L/4/
or
DATA I,J,K,L/1,2,3,4/

note that DATA gives variables the SAVE attribute,
and that they are initialized only once.

-- glen

Reply With Quote
  #8  
Old 08-20-2008, 11:50 PM
tju329
Guest
 
Default Re: common data in fortran77

On Aug 20, 6:37*pm, David Flower <DavJFlo...@aol.com> wrote:
> On Aug 20, 11:22*am, tju329 <tju...@gmail.com> wrote:
>
>
>
>
>
> > On Aug 20, 5:45*pm, Arjen Markus <arjen.mar...@wldelft.nl> wrote:

>
> > > On 20 aug, 11:28, tju329 <tju...@gmail.com> wrote:

>
> > > > I have 50 different integer variables for different purpose (so they
> > > > are not stored in an integer array). After initialization, they will
> > > > be used in many subroutines. Then, I need to declare all these 50
> > > > variables, and initialized them locally in each subroutine. It’s
> > > > terrible to code in this way since it wastes many lines but do nothing
> > > > different, and the code is not beautiful. Could somebody suggest any
> > > > solutions?

>
> > > > Thanks,
> > > > Cheng’an

>
> > > Are you stuck with FORTRAN 77 or can you use Fortran 90/95?

>
> > > In the latter case, use a module that contains these variables
> > > and use them in the various routines.

>
> > > If you are stuck with FORTRAN 77 for a good and solid reason (*),
> > > then COMMON blocks are most likely what you are looking for.
> > > Put the declarations and the definition of the COMMON blocks
> > > in a separate file so that you can use the INCLUDE statement
> > > in each routine to, well, include these declarations and
> > > definitions.

>
> > > Regards,

>
> > > Arjen

>
> > > (*) There are several free Fortran 90/95 compilers available,
> > > * * so not being able to buy one, is not really an excuse.

>
> > Hi Arjen,

>
> > Thanks for your reply and suggestions.

>
> > I have to use FORTRAN77. I forgot a very important thing in the last
> > post, i.e. I can not initialize these variables in simple way like
> > “length = 111”, instead I need to call a subroutine to get them out
> > from a “large database”. Common block can not work in my case. One
> > could say you can get them out once and reuse them repeatly later on,
> > but the problem is some of the variables could be changed by other
> > subroutines those are out of my control. Sync is necessary and should
> > be in the first step in my subroutines.

>
> > Thanks,

>
> > Cheng'an- Hide quoted text -

>
> > - Show quoted text -

>
> But if they are always the same variables, you just need a SUBROUTINE
> to either:
>
> a) Get them out of the database (again)
>
> or
>
> b) Restore the contents of the COMMON block to the status quo (in
> which case you will still need (a), and a routine to store the data
> somewhere
>
> Dave Flower- Hide quoted text -
>
> - Show quoted text -


Yes. Exactly.

What I need is
(a) a header file that declares all variables
(b) a subroutine that sync my variables to the latest status

Then, in all my functional subroutines, I can include the header file
and call the sync subroutine.

It works, :-)

Thank you all.

Cheng'an
Reply With Quote
  #9  
Old 08-21-2008, 12:03 AM
tju329
Guest
 
Default Re: common data in fortran77

On Aug 21, 4:37*am, glen herrmannsfeldt <g...@ugcs.caltech.edu> wrote:
> tju329 wrote:
> > I have 50 different integer variables for different purpose (so they
> > are not stored in an integer array). After initialization, they will
> > be used in many subroutines. Then, I need to declare all these 50
> > variables, and initialized them locally in each subroutine. It’s
> > terrible to code in this way since it wastes many lines but do nothing
> > different, and the code is not beautiful. Could somebody suggest any
> > solutions?

>
> I am not so sure what you are trying to do, but...
>
> If you have a large number of separate variables that you
> need to pass around to many routines, you could put them in
> a structure, though that requires features past Fortran 77.
>
> For Fortran 77, you can EQUIVALENCE them to array elements,
> and pass the array around. *They must be the same type, though.
> You can't EQUIVALENCE to dummy arguments, though.
>
> Also, you can initialize variables in a DATA statement.
> It isn't the same as an assignment statement, but DATA often
> works better for initializing many variables (or arrays).
>
> * * * *DATA I/1/,J/2/,K/3/,L/4/
> or
> * * * *DATA I,J,K,L/1,2,3,4/
>
> note that DATA gives variables the SAVE attribute,
> and that they are initialized only once.
>
> -- glen


EQUIVALENCE can avoid passing massive number of separated variables by
using array. However, an additional include file is necessary which
defines macros for all the separated variables. In this way, one will
not worry about situations like removing /adding a variable from/to
the array, which will change the array index for the separated
variables, in future development.

-- Cheng'an
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 03:01 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.