experchange > fortran

ygal klein (07-06-19, 08:27 PM)
Hi,
Is the following way to assign values to an array using brackets as array constructors against the standard?

The following code is compiled successfully using intel compiler 2019 but gfortran 9.1 results with a compilation error stating that there is a 'syntax error in array constructor'.

Example code:

program test
implicit none
integer, parameter :: length
real(8), dimension(length) :: a
a(:) = [1:length]
write(*, *) a(:)
end program test

Intel compiler succeedes in compiling and gfortran fails.

I would be glad to know why is that.

Thanks!
Richard Weed (07-06-19, 09:12 PM)
On Saturday, July 6, 2019 at 1:28:01 PM UTC-5, ygal klein wrote:
[..]
> Intel compiler succeedes in compiling and gfortran fails.
> I would be glad to know why is that.
> Thanks!


Unless this is something new in Fortran 2018, I think Intel is wrong but you
never know.

The safest thing to do is the standard way ie

replace

a(:) = [1:length]

with

integer :: i

a(:) = [(REAL(i,8), i=1,length)]

assuming length actually has a value which it doesn't in your
example. The brackets are just a slightly easier way of using array constructors compared to the original f90 syntax ie (/ /). Don't assume they work like matlab or other languages however even though they look like matlab's constructor syntax.
ygal klein (07-06-19, 09:15 PM)
On Saturday, July 6, 2019 at 10:12:26 PM UTC+3, Richard Weed wrote:
[..]
> a(:) = [(REAL(i,8), i=1,length)]
> assuming length actually has a value which it doesn't in your
> example. The brackets are just a slightly easier way of using array constructors compared to the original f90 syntax ie (/ /). Don't assume they work like matlab or other languages however even though they look like matlab's constructor syntax.


Hi,
First, Thanks!

Second, I am sorry for the typo in assigning length a value.
Assume length has been assigned.

Intel - succeeds (!) in compiling and the result seems right.
gfortran does not compile.

Is the way I wrote is against the standard?

Thanks.
steve kargl (07-06-19, 09:59 PM)
ygal klein wrote:

> On Saturday, July 6, 2019 at 10:12:26 PM UTC+3, Richard Weed wrote:
> Hi,
> First, Thanks!
> Second, I am sorry for the typo in assigning length a value.
> Assume length has been assigned.
> Intel - succeeds (!) in compiling and the result seems right.
> gfortran does not compile.
> Is the way I wrote is against the standard?


yes. Read what Richard wrote.
Similar Threads