Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outputing an allocatable array from a subroutine

Status
Not open for further replies.

UCDavisPrgmer

Technical User
Feb 20, 2005
2
US
-just can't seem to do it.

My goal: make a simple subroutine that reads in a text file with 1 column of numbers and outputs an array with those numbers.

I can do it no problem in the main program but have been going nuts trying to output an array for which I don't know the dimensions in advance. I know you can use the allocatable inside a procedure, but I haven't seen it used when the array is an output of the procedure.

I've tried assumed shape and assumed size arrays but have had no luck.

The code I would like to put into a subroutine (that works in the main) is found below.

thanks for any help,
-J

*******************************************************
!_____________________________________________________
! Declaration of Variables
CHARACTER(len=20):: InputFile = 'keran.dat'
INTEGER::status ! status = 0 for success
INTEGER::nflows ! No. of flow data values
REAL:: temp ! temp var to read in data
! Arrays:
REAL, Allocatable, dimension:)) :: FlowArray
!______________________________________________________

! Open input file:
OPEN (unit=20, File=InputFile, STATUS='OLD', ACTION='READ', &
POSITION='REWIND',IOSTAT=status)
! Check if file is open
fileopen: If (status /= 0) THEN
WRITE (*,*) 'Hey, I failed to open the file'
GO TO 999
END IF fileopen
! Open was succesful -> count data length to size array
DO
READ(20, *,IOSTAT=status) temp
IF (status /= 0) EXIT
nflows = nflows + 1
END DO
! Read input file into an array
ALLOCATE ( FlowArray(nflows),STAT=status) ! allocate memory
REWIND (Unit=20) ! rewind file, go back to beginning
READ (20,*) FlowArray ! read in data and place into array
 
The array should be declared as allocatable at the initial declaration and where it is being allocated/freed. See if this works on your system
Code:
module BoxMod
contains
   subroutine BoxCreate (blist, bsize)
      integer, allocatable, intent (inout) :: blist(:)
      integer, intent (in) :: bsize
      integer :: b
      allocate (blist(bsize))
      do b = 1, bsize
         blist(b) = 0
      enddo
      return
   end subroutine BoxCreate

   subroutine BoxDelete (blist)
      integer, allocatable, intent (inout) :: blist(:)
      deallocate (blist)
      return
   end subroutine BoxDelete
end module BoxMod

program main
   use BoxMod
   integer, dimension(:), allocatable :: alist
   integer:: amaxi
   parameter (amaxi = 20)
   call BoxCreate (alist, amaxi)
   do i = 1, amaxi, 1
      alist(i) = i * i
   enddo

   do i = 1, amaxi, 1
      write (*,*) alist(i)
   enddo
   call BoxDelete (alist)
   stop
end program main
 
I get this compiler error. I have been trying to implement a simmilar problem with no good prospects, any other hint?.

Regards

fishy

cf90-425 f90fe: ERROR READPOT, File = SRC/jedi.f90, Line = 27, Column = 33
Attributes INTENT and ALLOCATABLE must not appear in the same attribute list.
 
Looks like a F90 problem that has been fixed on F95. I'll see if I can find a F90 compiler and if there is a way around it.
 
Thanks for the repply!.

My current program looks uggggggly, as I had to put a huge piece of code in an otherwise smoot main program, . . . nevertheless, I tried to compile with absoft f95, and no good luck either :( , . . . I am curious about what compiler are you using...


fishy
 
I'm using g95. I just tried the old Lahey F90 compiler which I downloaded when it was free. That doesn't allow anything: no pointers or allocatable stuff can be passed as parameters. The only way I can get it to work is to put the allocatables at module level but then it makes it a global which defeats the purpose of making it a parameter in the first place. Anyway here's my two pennies worth
Code:
module BoxMod
   implicit none ! reqd by Lahey
   integer, allocatable:: alist(:)  ! put this at module level
contains
   subroutine BoxCreate (bsize)
      integer, intent (in) :: bsize
      integer :: b
      write (*,*) 'Allocating ', bsize
      allocate (alist(bsize))
      do b = 1, bsize
         alist(b) = 0
      end do
      return
   end subroutine BoxCreate

   subroutine BoxDelete ()
      write (*,*) 'Deallocating'
      deallocate (alist)
      return
   end subroutine BoxDelete
end module BoxMod

program main
   use BoxMod
   implicit none ! reqd by Lahey
   integer:: amaxi, i
   ! parameter (amaxi = 20)  ! Lahey doesn't like PARAMETER
   amaxi = 20
   call BoxCreate (amaxi)
   do i = 1, amaxi, 1
      alist(i) = i * i
   end do

   do i = 1, amaxi, 1
      write (*,*) alist(i)
   end do
   call BoxDelete ()
   stop
end program main
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top