0

I saw this post about making a ragged array.

when I try to do that everything work until I want to access this array.

type :: vector
    integer, dimension(:), allocatable :: elements
end type vector
type :: ragged_array
    type(vector), dimension(:), allocatable :: vectors
end type ragged_array
type(ragged_array) :: ragarr
allocate(ragarr%vectors(1)%elements(3))
allocate(ragarr%vectors(2)%elements(4))
!PROBLEM HERE :
raggar(1,1:3)=0
raggar(2,1:4)=1

It give me error :

The assigment operation or the binary expression operation is invalid for the data type of two operands

It's still unclear for me how to manipulate this ragged array, how do I access a specific value... thanks for any help !

Dadep
  • 2,796
  • 5
  • 27
  • 40

1 Answers1

2

Your code contains many errors:

  1. You should allocate raggar%vectors before allocate its components raggar%vectors%elements.
  2. raggar is a scalar which contains allocatable array which contains allocatable array and it is not an array, if you want to access its elements you can only use raggar%vectors(i)%elements(j)

Corrected code:

type :: vector
    integer, dimension(:), allocatable :: elements
end type vector

type :: ragged_array
    type(vector), dimension(:), allocatable :: vectors
end type ragged_array

type(ragged_array) :: ragarr

allocate( raggar%vectors(2) )
allocate( ragarr%vectors(1)%elements(3) )
allocate( ragarr%vectors(2)%elements(4) )

!PROBLEM HERE :
raggar%vectors(1)%elements=0 !raggar(1,1:3)=0
raggar%vectors(2)%elements=0 !raggar(2,1:4)=1
M. Chinoune
  • 409
  • 5
  • 10
  • And Is there a way to take the entire `raggar` to pass it through an argument of subroutine ? it's seems that doing `raggar%vectors%elements` does not work.... – Dadep Jun 06 '17 at 13:48