Array Initialization: Difference between revisions
Content added Content deleted
(→AWK: Also looks to be covered) |
(Fortran, Perl, and Smalltalk were already noted as being covered,) |
||
Line 99: | Line 99: | ||
std::vector v4 = v3; // v4 is a copy of v3 |
std::vector v4 = v3; // v4 is a copy of v3 |
||
</lang> |
</lang> |
||
==[[Fortran]]== |
|||
{{works with|Fortran|95 and later}} |
|||
Arrays can be initialized using a robust set of array constructors and array intrinsic functions. |
|||
<lang fortran> program arrays |
|||
real :: a(100) = 0 ! entire array initialized to ZERO |
|||
real :: b(9) = (/ 1,2,3,4,5,6,7,8,9 /) ! array constructor |
|||
real :: c(10) = (/ (2**i, i=1, 10) /) ! array constructor with "implied do loop" |
|||
! An array constructor is a literal definition of a ONE-DIMENSIONAL array only. |
|||
! In order to initialize multidimensional arrays, you need to use the RESHAPE instrinsic |
|||
! function: |
|||
real :: d(2, 5) = reshape( (/ (2**i,i=1,10) /), (/ 2, 5 /) ) |
|||
! ^^^^^^^^^^^^^^^^^^^source array ^^^^^^^^^^ dimension shape |
|||
! Fills the array in COLUMN MAJOR order. That is, traverse the first dimension first, |
|||
! second dimension second, etc. |
|||
! In order to fill a matrix in ROW MAJOR order, merely specify as before, |
|||
! but TRANSPOSE the result |
|||
real :: e(4, 4) = transpose( reshape( & |
|||
(/ 1, 2, 3, 4, & |
|||
5, 6, 7, 8, & |
|||
9, 10, 11, 12, & |
|||
13, 14, 15, 16 /), (/ 4, 4 /) ) ) |
|||
print *, b |
|||
print * |
|||
print *, c |
|||
print * |
|||
print '(5F7.0)', d(1,:) ! print row 1 |
|||
print '(5F7.0)', d(2,:) ! print row 2 |
|||
print * |
|||
do i = 1, 4 |
|||
print '(4F7.0)', e(i,:) ! print row I |
|||
end do |
|||
end program arrays</lang> |
|||
Output: |
|||
1. 2. 3. 4. 5. 6. 7. 8. 9. |
|||
2. 4. 8. 16. 32. 64. 128. 256. 512. 1024. |
|||
2. 8. 32. 128. 512. |
|||
4. 16. 64. 256. 1024. |
|||
1. 2. 3. 4. |
|||
5. 6. 7. 8. |
|||
9. 10. 11. 12. |
|||
13. 14. 15. 16. |
|||
Here is a more interesting example showing a function that creates and returns a square identity matrix of order N: |
|||
<lang fortran> module matrixUtil |
|||
contains |
|||
function identity(n) |
|||
integer, intent(in) :: n |
|||
integer :: i, j |
|||
real, pointer :: identity(:,:) |
|||
allocate(identity(n,n)) |
|||
! the MERGE intrinsic is like a C ternary (if-then-else) operator, except that the logical condition |
|||
! comes at the end, it is a logical ARRAY, and the function result is an array of the same size and shape |
|||
identity = merge (1.0, 0.0, reshape( (/ ( (i==j, i=1,n), j=1,n) /), (/ n, n /) ) ) |
|||
! ^^^ if-TRUE-value ^^^ if-FALSE-value ^^<-NxN logical array, true only on the diagonal->^^ |
|||
! The TVALUE and FVALUE must be "conformable" to the logical array, meaning that each is either: |
|||
! -- an array of the same size and shape as the logical array |
|||
! -- a scalar |
|||
end function identity |
|||
end module matrixUtil</lang> |
|||
<lang fortran> program muTest |
|||
use matrixUtil |
|||
integer :: n |
|||
real, pointer :: i(:,:) |
|||
read (*,*) n |
|||
i => identity(n) |
|||
do j=1, size(i,1) |
|||
print *, i(j,:) |
|||
end do |
|||
deallocate(i) |
|||
end program muTest</lang> |
|||
Example run: |
|||
$ <span style="color: blue;">g95 -o mu matrixUtil.f95 muTest.f95</span> |
|||
$ <span style="color: blue;">./mu</span> |
|||
<span style="color: red;">15</span> |
|||
1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. |
|||
0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. |
|||
0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. |
|||
0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. |
|||
0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. |
|||
0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. |
|||
0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. |
|||
0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. |
|||
0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. |
|||
0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. |
|||
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. |
|||
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. |
|||
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. |
|||
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. |
|||
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. |
|||
$ |
|||
==[[F_Sharp|F#]]== |
==[[F_Sharp|F#]]== |
||
Line 321: | Line 219: | ||
VAR arr: Vector; |
VAR arr: Vector; |
||
arr := Vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};</lang> |
arr := Vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};</lang> |
||
== [[Perl]]== |
|||
Perl only has three types: Scalar, Array, Hash Array |
|||
<lang perl> |
|||
use strict; |
|||
use warnings; |
|||
my @intArray = ( 1, 2, 3, 4, 5, 6, 7 ); |
|||
$intArray[0] = 1; |
|||
my @strArray = qw( aaa bbb ccc ddd eee ); |
|||
$strArray[1]= "bbb"; |
|||
my @matrix = ([qw( 00 01 12)], [qw(10 11 12)], [qw(20 21 22)]); |
|||
$matrix[1][2]= "12"; |
|||
my %hashArray = ("key1", "value1", "key2", "value2", "key3", "value3"); |
|||
my %hashArray2 = ("key1"=>"value1", "key2"=>"value2", "key3"=>"value3"); |
|||
$hashArray2 {"key2"}="value2"; |
|||
</lang> |
|||
==[[Python]]== |
==[[Python]]== |
||
Line 375: | Line 256: | ||
#(1 b 34) "is quoted... array at: 1 will return #b" |
#(1 b 34) "is quoted... array at: 1 will return #b" |
||
</lang> |
</lang> |
||
==[[Smalltalk]]== |
|||
<lang smalltalk>|array| |
|||
array := Array withAll: #('an' 'apple' 'a' 'day' 'keeps' 'the' 'doctor' 'away'). |
|||
"Access the first element of the array" |
|||
elem := array at: 1. |
|||
"Replace apple with orange" |
|||
array at: 2 put: 'orange'.</lang> |