From: wangning16934 on
I had already produce an open file with data in the main program, but
I want to use the data in the file in the subroutine. However,it seems
didn't use when I call it. I don't know what's wrong,please help me!
Thank you very much!

subroutine get_trio

.......

open(111, file="surf_111_3.dat")
open(112, file="random.dat")

do i=1,200
read (111,*) k, posx(i),
posy(i)
end do

read(111,*) a
do i =1, 25
write (112,"(25( i3,f6.1,f6.1))") a(i), posx(a(i)), posy(a(i))
end do

.......
do i = 1, n
do j = i + 1, n
rxij = posx(a(i)) - posx(a(j))
if (abs(rxij) .gt. l/2) then
rxij = l - abs(rxij)
end if
ryij = posy(a(i)) - posy(a(j))
if (abs(ryij) .gt. l/2) then
ryij = l - abs(ryij)
end if
disij = sqrt(rxij**2 + ryij**2)
.......
It said that a scalar-valued expression is required in this context.
From: Klaus Wacker on
wangning16934(a)gmail.com wrote:
> I had already produce an open file with data in the main program, but
> I want to use the data in the file in the subroutine. However,it seems
> didn't use when I call it. I don't know what's wrong,please help me!
> Thank you very much!
>

It is rather unusual to use an externel file to pass data to a
subroutine. However, if you must do it that way, you will have to
close the file before you attempt to read from it.

The normal way is to use arguments to pass data to a subroutine. Other
possibilities are to use modules or common blocks.

> subroutine get_trio
>
> .......
>
> open(111, file="surf_111_3.dat")
> open(112, file="random.dat")
>
> do i=1,200
> read (111,*) k, posx(i),
> posy(i)
> end do
>
> read(111,*) a
> do i =1, 25
> write (112,"(25( i3,f6.1,f6.1))") a(i), posx(a(i)), posy(a(i))
> end do
>
> .......
> do i = 1, n
> do j = i + 1, n
> rxij = posx(a(i)) - posx(a(j))
> if (abs(rxij) .gt. l/2) then
> rxij = l - abs(rxij)
> end if
> ryij = posy(a(i)) - posy(a(j))
> if (abs(ryij) .gt. l/2) then
> ryij = l - abs(ryij)
> end if
> disij = sqrt(rxij**2 + ryij**2)
> ......
> It said that a scalar-valued expression is required in this context.

Impossible to say what the problem here is if you don't show the
declarations of your variables. My advice would be to use IMPLICIT
NONE and declare all your variables. If you still have problems, post
the whole subroutine.

--
Klaus Wacker klaus.wacker(a)udo.edu
Experimentelle Physik V http://www.physik.uni-dortmund.de/~wacker
Universitaet Dortmund Tel.: +49 231 755 3587
D-44221 Dortmund Fax: +49 231 755 4547
From: blitheli on
On 4ÔÂ23ÈÕ, ÏÂÎç4ʱ55·Ö, wangning16...(a)gmail.com wrote:
> I had already produce an open file with data in the main program, but
> I want to use the data in the file in the subroutine. However,it seems
> didn't use when I call it. I don't know what's wrong,please help me!
> Thank you very much!
>
> subroutine get_trio
>
> .......
>
> open(111, file="surf_111_3.dat")
> open(112, file="random.dat")
>
> do i=1,200
> read (111,*) k, posx(i),
> posy(i)
> end do
>
> read(111,*) a
> do i =1, 25
> write (112,"(25( i3,f6.1,f6.1))") a(i), posx(a(i)), posy(a(i))
> end do
>
> .......
> do i = 1, n
> do j = i + 1, n
> rxij = posx(a(i)) - posx(a(j))
> if (abs(rxij) .gt. l/2) then
> rxij = l - abs(rxij)
> end if
> ryij = posy(a(i)) - posy(a(j))
> if (abs(ryij) .gt. l/2) then
> ryij = l - abs(ryij)
> end if
> disij = sqrt(rxij**2 + ryij**2)
> ......
> It said that a scalar-valued expression is required in this context.

I think it is better to write like this:

Module Constants
implicit none
public

integer,parameter:: file_handle=11,file_handle2=12 ! you can add more
parameter file handle here

character(len=*), parameter :: file_name, file_name2 ! you can add
more parameter names here

End module Constants

!------------------------

program main
implicit none
....

open(unit=file_handle,file=file_name)
open(unit=file_handle2,file=file_name2)
...
call subr()
...
close(file_handle)
close(file_handle2)
end
!--------------------
subroutine subr(a,b,...)
implicit none
...
...
write(unit=file_handle,fmt=...)a,b.....
....
End subroutine subr


From: Terence on
And if you use a direct access file (as I do a lot when matrix data
exceeds memory possibilities), you can write to the end of the file,
but also read and search on the same file in the subroutine or even
the same main. Though it is best to save the last record number as a
global variable to know where to continue writing from.