Multi-dimensional array: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(6 intermediate revisions by 4 users not shown)
Line 785:
[5, 4, 4]
[5, 5, 5]]</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|}}
 
 
<syntaxhighlight lang="Delphi">
{1. Delphi support arrays of any number of dimensions, including non-orthogonal arrays.}
{2. Delphi arrays are Row Major.}
{3. This creates a five dimensional array in Delphi.}
 
var MyArray: array [0..5,0..4,0..3,0..2] of integer;
 
{4. This reads and writes array items:}
 
MyArray[5,4,3,2]:=MyArray[1,2,3,2];
 
{5. Array memory space can be compacted to guarantee that all items are contiguous.}
 
var MyArray3: packed array [0..5,0..4,0..3,0..2] of integer;
 
{6. You can create array that start at indices other than zero.}
 
var MyArray2: array [5..25,26..50] of integer;
 
{7. You can create non-orthogonal arrays:}
 
var A : array of array of string;
var I, J : Integer;
begin
SetLength(A, 10);
for I := Low(A) to High(A) do
begin
SetLength(A[I], I);
for J := Low(A[I]) to High(A[I]) do
A[I,J] := IntToStr(I) + ',' + IntToStr(J) + ' ';
end;
end;
 
</syntaxhighlight>
{{out}}
<pre>
 
</pre>
 
 
=={{header|EchoLisp}}==
Line 1,153 ⟶ 1,198:
 
For large arrays when memory is not truly random-access (on-chip memory versus main memory, main memory versus slower disc storage) a calculation that works along a row will be accessing widely-separated elements in storage, perhaps with only one "hit" per virtual memory page before leaping to another, while an equivalent calculation working down a column will enjoy multiple hits per page. The difference can be large enough to promote usages such as swapping the order of the DO-loops for portions of a calculation, or swapping the order of the indices as by writing <code>A(J,I)</code> when it would be more natural to use <code>A(I,J)</code>.
 
=={{header|FreeBASIC}}==
Multidimensional arrays can be declared as well, and are stored in this
definite order: values differing only in the last index are contiguous
(row-major order).
The maximum number of dimensions of a multidimensional array is 8.
 
<syntaxhighlight lang="vbnet">
'' declare a three-dimensional array of single
'' precision floating-point numbers.
Dim array(1 To 2, 6, 3 To 5) As Single
 
'' The first dimension of the declared array
'' has indices from 1 to 2, the second, 0 to 6,
'' and the third, 3 to 5.
</syntaxhighlight>
 
The total number of elements and the total size (in bytes) of a multi-dimensional
array can be directly obtained using Arraylen and Arraysize, respectively.
 
<syntaxhighlight lang="vbnet">
' Take Care while initializing multi-dimensional array
Dim As Integer multidim(1 To 2, 1 To 5) = {{0,0,0,0,0}, {0,0,0,0,0}}
</syntaxhighlight>
 
<syntaxhighlight lang="vbnet">Dim As Integer A(5,4,3,2)
A(3,1,0,1) = 3100
A(3,1,0,1) = A(3,1,0,1)+1
Print A(3,1,0,1)</syntaxhighlight>
{{out}}
<pre>3101</pre>
 
=={{header|Go}}==
Line 2,228 ⟶ 2,304:
[ 0, 0, 0, 0, 0]]]], dtype=int8)
>>> </syntaxhighlight>
 
=={{header|Quackery}}==
 
Quackery does not come supplied with multi-dimensional arrays. Presented here are two idiomatic implementations, the first using nests of nests, and the second using row major.
 
===Utility words common to both implementations===
 
When an <code>array</code> is created, each element of the array is preloaded with a datum. It could be initialised with something appropriate, such as e.g. zeros or empty strings, or <code>null</code> to indicate no value has been assigned yet.
 
When an <code>array</code> is created, its layout needs to be specified as a nest of numbers. For the array used in this task the layout is <code>[ 5 4 3 2 ]</code>.
 
Individual items in an array are also referred to with a nest of numbers, a "specifier". For the array used in this task the specifier will be four numbers, in the ranges <code>0...4</code>, <code>0...3</code>, <code>0...2</code>, and <code>0...1</code>, i.e. <code>[ 0 0 0 0 ] ... [ 4 3 2 1 ]</code>.
 
<code>[a]-></code>takes a specifier and a layout, and returns a number equal to the position of the item if the array were in row major.
 
<code>->[a]</code> is the converse operation; it takes a number and a layout, and returns a specifier.
 
To determine the number of items in an array from its layout, use <code>1 swap witheach *</code>
 
Note that in the nest-of-nests version arrays do not carry their layouts with them, so you would need to keep a note of the layout of an array somewhere if it will be required (i.e for the following example use.)
 
An example use for these would be using a <code>times</code> loop to iterate through every item of a multidimensional array without requiring nested <code>times</code> loops.
 
So <code>' [ 0 1 1 0 ] ' [ 5 4 3 2 ] [a]-></code> returns <code>8</code>, and <code>8 ' [ 5 4 3 2 ] ->[a]</code> returns <code>[ 0 1 1 0 ]</code>.
 
<syntaxhighlight lang="Quackery"> [ this ] is null ( --> [ )
 
[ 0 unrot
witheach
[ dip
[ behead rot ]
* + swap ]
drop ] is [a]-> ( [ [ --> n )
[ [] unrot
reverse
witheach
[ /mod rot
join swap ]
drop ] is ->[a] ( n [ --> [ )
</syntaxhighlight>
 
===Nest of nests===
 
<code>array</code> takes a datum with with to initialise each item in the array, and a layout, as described above, and returns an array.
 
<code>[peek]</code> takes a specifier as described above, and an array, and returns a datum.
 
<code>[poke]</code> takes a datum, an array, and a specifier, and returns an updated array, with the new datum in the specified location.
 
<syntaxhighlight lang="Quackery"> [ dip nested
reverse
witheach
[ of i if nested ] ] is array ( [ --> a )
 
[ witheach peek ] is [peek] ( [ a --> x )
 
[ dup dip
[ rot dip
[ -1 split drop
witheach
[ dip dup peek ] ] ]
reverse
witheach
[ dip swap poke ] ] is [poke] ( x a [ --> a )
</syntaxhighlight>
 
===Row major===
 
<code>array</code>, <code>[peek]</code>, and <code>[poke]</code> have the same behaviours as the nest-of-nests versions above.
 
<code>[layout]</code> takes an array and returns its layout.
 
<code>[size]</code> takes an array and returns the number of items in the array.
 
<syntaxhighlight lang="Quackery"> [ swap nested
over 1 swap
witheach *
of
2 pack ] is array ( x [ --> [ )
 
[ 0 peek ] is [layout] ( [ --> [ )
 
[ 1 peek size ] is [size] ( [ --> n )
 
[ swap unpack
unrot [a]-> peek ] is [peek] ( [ a --> x )
 
[ swap unpack
dip [ tuck [a]-> ]
rot dip
[ swap poke ]
swap 2 pack ] is [poke] ( x a [ --> a )</syntaxhighlight>
 
===Task===
 
The task code and output are the same for both versions.
 
<syntaxhighlight lang="Quackery"> null ' [ 5 4 3 2 ] array
say "[ 5 4 3 2 ] array created, initialised with nulls."
cr
cr
2 swap ' [ 3 2 1 0 ] [poke]
say "2 stored in [ 3 2 1 0 ]."
cr
4 swap ' [ 4 3 2 1 ] [poke]
say "4 stored in [ 4 3 2 1 ]."
cr
8 swap ' [ 4 3 2 1 ] [poke]
say "8 stored in [ 4 3 2 1 ]."
cr
cr
dup ' [ 0 0 0 0 ] [peek] echo
say " copied from [ 0 0 0 0 ]."
cr
dup ' [ 3 2 1 0 ] [peek] echo
say " copied from [ 3 2 1 0 ]."
cr
' [ 4 3 2 1 ] [peek] echo
say " copied from [ 4 3 2 1 ]."
cr</syntaxhighlight>
 
{{out}}
 
<pre>[ 5 4 3 2 ] array created, initialised with nulls.
 
2 stored in [ 3 2 1 0 ].
4 stored in [ 4 3 2 1 ].
8 stored in [ 4 3 2 1 ].
 
null copied from [ 0 0 0 0 ].
2 copied from [ 3 2 1 0 ].
8 copied from [ 4 3 2 1 ].</pre>
 
=={{header|Racket}}==
Line 2,516 ⟶ 2,725:
The interested reader should also be aware of the difference between arrays and [http://www.tcl.tk/man/tcl/TclCmd/dict.htm dict]ionaries, and know that the latter are often preferred for record-like structures.
 
=={{header|V (Vlang)}}==
 
<syntaxhighlight lang="v (vlang)">smd := [][]string{len: 4, init: []string{len:2, init: 'Hello'}}
imd := [][]int{len: 3, init: []int{len:4, init: it}}
mut mmd := [][]f64{len: 5, init: []f64{len: 5}}
Line 2,545 ⟶ 2,754:
 
Otherwise what was said in the Kotlin preamble applies more or less to Wren.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
// create a 4 dimensional list of the required size and initialize successive elements to the values 1 to 120
9,485

edits