문제

Is there a way where I can assign the same value for different variables without constructing an array in a single statement?

For example, if I have variables a,b,c,d, and e, can I assign something like

a=b=c=d=e=10.0 

?

I know that I can do in a single line:

a=10.0; b=10.0; c=10.0; d=10.0; e=10.0

But that is not I want since if I want to change the value 10.0 later to something else, I have to make the change everywhere.

도움이 되었습니까?

해결책 2

Come on Fortranners, you know you want to ...

equivalence(a,b,c,d,e)

Now all those rascals are going to have the same value at all times.

다른 팁

The first version is not possible in Fortran. Following the (2008) Standard, an assignment is of the general form (ch. 7.2.1.1)

variable = expr

But why don't you try something like:

a=10.0; b=a; c=a; d=a; e=a

That way, you just need to change the value of a later on!

Perhaps:

real, parameter :: NamedConst = 10.0

a=NamedConst; b=NamedConst; c=NamedConst; d=NamedConst; e=NamedConst

Then if you should use the special value NamedConst in more than one line, there is clearly a single place to change its value.

You probably should consider to use an array instead of individual variables, especially if they serve similar purposes:

real :: myarray(5)

myarray(:) = 10.0
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top