[UP]


Manual Reference Pages  - array_constructors (7)

NAME

Array_Constructors(7f) - [FORTRAN] An array constructor can be used to create and assign values to rank-one arrays (and array constants) (LICENSE:PD)

SYNTAX

An array constructor takes the following form:

   (/ac-value-list/)
   [ac-value-list]

DESCRIPTION

OPTIONS

ac-value-list
  Is a list of one or more expressions or implied-DO loops. Each ac-value must have the same type and kind parameters, and be separated by commas.
implied-do
  An implied-DO loop in an array constructor takes the following form:
                (ac-value-list, do-variable = expr1, expr2 [,expr3])

do-variable
  Is the name of a scalar integer variable. Its scope is that of the implied-DO loop.

expr Is a scalar integer expression. The expr1 and expr2 specify a range of values for the loop; expr3 specifies the stride. The expr3 must be a nonzero value; if it is omitted, it is assumed to be 1.

Description

The array constructed has the same type as the ac-value-list expressions.

If the sequence of values specified by the array constructor is empty (an empty array expression or the implied-DO loop produces no values), the rank-one array has a size of zero.

An ac-value is interpreted as follows:

Form of ac-value
  Result
A scalar expression
  Its value is an element of the new array.
An array expression
  The values of the elements in the expression (in array element order) are the corresponding sequence of elements in the new array.
An implied-DO loop
  It is expanded to form a list of array elements under control of the DO variable (like a DO construct).
The following shows the three forms of an ac-value:
  C1 = (/4,8,7,6/)                  ! A scalar expression
  C2 = (/B(I, 1:5), B(I:J, 7:9)/)   ! An array expression
  C3 = (/(I, I=1, 4)/)              ! An implied-DO loop

You can also mix these forms, for example:
  C4 = (/4, A(1:5), (I, I=1, 4), 7/)

If every expression in an array constructor is a constant expression, the array constructor is a constant expression.
If the expressions are of type character, Fortran 95/90 requires each expression to have the same character length.
However, Intel Fortran allows the character expressions to be of different character lengths. The length of the resultant character array is the maximum of the lengths of the individual character expressions. For example:
   print *,len ( (/’a’,’ab’,’abc’,’d’/) )
   print *,’++’//(/’a’,’ab’,’abc’,’d’/)//’--’

This causes the following to be displayed:
 

3

++a --++ab --++abc--++d --

If an implied-DO loop is contained within another implied-DO loop (nested), they cannot have the same DO variable (do-variable).

To define arrays of more than one dimension, use the RESHAPE intrinsic function.

The following are alternative forms for array constructors:

Square brackets (instead of parentheses and slashes) to enclose array constructors; for example, the following two array constructors are equivalent:

  INTEGER C(4)
  C = (/4,8,7,6/)
  C = [4,8,7,6]

EXAMPLES

The following example shows an array constructor using an implied-DO loop:

  INTEGER ARRAY_C(10)
  ARRAY_C = (/(I, I=30, 48, 2)/)

The values of ARRAY_C are the even numbers 30 through 48.

Implied-DO expressions and values can be mixed in the value list of an array constructor. For example:

 INTEGER A(10)
 A = (/1, 0, (I, I = -1, -6, -1), -7, -8 /)
 !Mixed values and implied-DO in value list.

This example sets the elements of A to the values, in order,

   1, 0, -1, -2, -3, -4, -5, -6, -7, -8.

The following example shows an array constructor of derived type that uses a structure constructor:

    TYPE EMPLOYEE

    INTEGER ID

CHARACTER(LEN=30) NAME

    END TYPE EMPLOYEE

TYPE(EMPLOYEE) CC_4T(4)
CC_4T = (/EMPLOYEE(2732, JONES ), EMPLOYEE(0217, LEE ),
  EMPLOYEE(1889,"RYAN"), EMPLOYEE(4339,"EMERSON")/)

The following example shows how the RESHAPE intrinsic function can be used to create a multidimensional array:

  E = (/2.3, 4.7, 6.6/)
  D = RESHAPE(SOURCE = (/3.5, (/2.0, 1.0/), E/), SHAPE = (/2,3/))

D is a rank-two array with shape (2,3) containing the following elements:

   3.5    1.0    4.7
   2.0    2.3    6.6

The following shows another example:

 INTEGER B(2,3), C(8)
 ! Assign values to a (2,3) array.
 B = RESHAPE((/1, 2, 3, 4, 5, 6/),(/2,3/))
 ! Convert B to a vector before assigning values to
 ! vector C.
 C = (/ 0, RESHAPE(B,(/6/)), 7 /)

Example using general purpose fortran routines

   program demo_array_constructors ! initializing small arrays
   use M_display, only : disp, disp_set
   implicit none
   integer :: i
   integer, parameter :: yy(*) = [  10,20,30  ,  40,50,60  ] ! make some data in a vector, could type this where yy appears below
   ! xx is same thing as yy, just using syntax for filling it that makes it clearer what I want to do with the data
   integer, parameter :: xx(*) = [ [10,20,30] , [40,50,60] ] ! make some data in a vector, could type this where xx appears below

integer, dimension(2,3)::aa = reshape(xx,shape(aa),order=[2,1]) ! 2d by rows using reshaped scalar expression integer, dimension(2,3)::bb = reshape(xx,shape(bb) ) ! 2d by columns integer, dimension(2,3)::cc = reshape(xx,shape(cc),order=[1,2]) ! 2d by columns integer, dimension(2,3)::dd = reshape([(i*10,i=1,size(dd))],shape(dd)) ! an implied do by columns

integer, dimension(2,3):: ff, gg, hh ! CANNOT DO !integer, dimension(2,3)::ff = [10,20,30,40,50,60 ] ! 2d by columns ! BUT CAN DO data ff/10,20,30,40,50,60/ ! fill 2D with simple data statement ! AND CAN DO ! multi-dimensional by equivalence integer :: ee(2,3) integer :: e(size(ee))=xx equivalence (e(1),ee(1,1)) ! CANNOT DO !integer, dimension(2,3)::gg = [10,20,30] , [40,50,60] !integer, dimension(2,3)::gg = [[10,20,30] , [40,50,60]] ! BUT CAN DO data gg(1,:)/ 10, 20, 30 / ! fill rows with data statements data gg(2,:)/ 40, 50, 60 /

data hh(:,1)/ 10, 40 / ! fill columns with data statements data hh(:,2)/ 20, 50 / data hh(:,3)/ 30, 60 / call disp_set(style=’left & number’)

write(*,*)’SIZE(aa)=’,size(aa) write(*,*)’SHAPE(aa)=’,shape(aa) write(*,*)’xx=’,xx write(*,*)’yy=’,yy

call disp(’aa=’,aa) call disp(’bb=’,bb) call disp(’cc=’,cc) call disp(’dd=’,dd)

call disp(’ee=’,ee)

call disp(’ff=’,ff) call disp(’gg=’,gg) call disp(’hh=’,hh)

write(*,*)repeat(’=’,80) write(*,*)hh write(*,*)repeat(’=’,80) call print_buildfmt(hh) write(*,*)repeat(’=’,80) call print_fixedfmt(hh)

contains

subroutine print_buildfmt(arr) use M_strings, only : v2s implicit none integer,intent(in) :: arr(:,:) integer :: i character(len=:),allocatable :: fmt fmt=’("> [",’//v2s(size(arr,dim=2))//’(i0:,","),"]")’ write(*,*)’FMT=’,fmt write(*,fmt)(arr(i,:),i=1,size(arr,dim=1)) end subroutine print_buildfmt

subroutine print_fixedfmt(arr) implicit none integer,intent(in) :: arr(:,:) integer :: i do i=1,size(arr,dim=1) write(*, ’("> [",*(i0:,","))’ ,advance=’no’)arr(i,:) write(*,’("]")’) enddo end subroutine print_fixedfmt

end program demo_array_constructors ! initializing small arrays

SEE ALSO

o DO construct
o Derived types
o Structure constructors
o Array Elements for details on array element order
o Array Assignment Statements for details on another way to assign values to arrays
o Declaration Statements for Arrays for details on array specifications


array_constructors (7) March 11, 2021
Generated by manServer 1.08 from 6d2b2bf1-845b-4b44-8ed6-0b5b3c5f0700 using man macros.