This comparison of programming languages (array) compares the features of array data structures or matrix processing for various computer programming languages.

Syntax

Array dimensions

The following list contains syntax examples of how to determine the dimensions (index of the first element, the last element or the size in elements).

Some languages index from zero. Some index from one. Some carry no such restriction, or even allow indexing by any enumerated type, not only integers.

LanguagesSizeFirstLast
Adaname'Lengthname'Firstname'Last
ALGOL 68UPB name - LWB name+1 2 UPB name - 2 LWB name+1 etc.LWB name 2 LWB name etc.UPB name 2 UPB name etc.
APLname (⍴ name)[index]⎕IO(⍴ name)-~⎕IO (⍴ name)[index]-~⎕IO
AWKlength1asorti
C#, Visual Basic (.NET), Windows PowerShell, F#name.Lengthname.GetLowerBound(dimension)name.GetUpperBound(dimension)
CFMLarrayLen(name) name.len()1name.len()
Chmax(shape(name))0max(shape(name))-1
Common Lisp(length name)0(1- (length name))
Dname.length0name.length-1 $-1
Fortransize(name)lbound(name)ubound(name)
Golen(name)0len(name) - 1
HaskellrangeSize (bounds name)fst (bounds name)snd (bounds name)
ISLISP(length name)0(1- (length name))
Icon*name1*name
Cobra, D, Haxe, Java, JavaScript, Scalaname.length0name.length - 1
J#name0<:@#name
JavaScript (ES2022)name.length0 name.at(0)name.length - 1 name.at(-1)
Julialength(name) size(name)begin first.(axes(name))end last.(axes(name))
Lingocount(name)1getLast(name)
LiveCodelength(name)1 first-1 last
Lua#name1 by convention; any integer#name
MathematicaLength[name]1 First[name]-1 Last[name]
MATLAB, GNU Octavelength(name)1end
Nimname.lenname.lowname.high
OberonLEN(name)0LEN(name) - 1
Object PascalLength(name)0 low(name)Length(name)-1 high(name)
Objective-C (NSArray * only)[name count]0[name count] - 1
OCamlArray.length name0Array.length name - 1
Perlscalar(@name)$[$#name
PHPcount(name)0count(name) - 1
PL/Idim(name[,dim])lbound(name[,dim])hbound(name[,dim])
Pythonlen(name)0-1 len(name) - 1
Rlength(name)1length(name)
Raku@name.elems0@name.end
Rebol, Redlength? namename/1 first namelast name
Rubyname.size0 name.first-1 name.size - 1 name.last
Rustname.len()0name.len() - 1
S-Langlength(name)0-1 length(name)-1
Scheme(vector-length vector)0(- (vector-length vector) 1)
Smalltalkname size1 name firstname size name last
Swiftname.count0name.count - 1
Unicon*name1*name
Visual BasicUBound(name)-LBound(name)+1LBound(name)UBound(name)
Wolfram LanguageLength[name]1 First[name]-1 Last[name]
XojoUBound(name)0UBound(name)
XPath/XQuerycount($name)1count($name) last() array:size(name)

Indexing

The following list contains syntax examples of how to access a single element of an array.

FormatLanguages
name[index] or name[index1, index2] etc.ALGOL 58, ALGOL 60, ALGOL 68, AWK, Julia, Modula, Pascal, Object Pascal, C#, S-Lang Icon, Unicon
name[index] or name[index1; index2] etc. or indexname or index1 index2name etc.APL
name[index]ActionScript, C, CFML, Ch, Cobra, C++, D, Go, Haxe, Java, JavaScript, Lingo, Lua, Nim, Objective-C (NSArray *), Perl, PHP, Python, R, Ruby, Rust, Swift
$name[index]Perl, Windows PowerShell, XPath/XQuery
@name[index]Raku
name(index) or name(index1, index2) etc.Ada, ALGOL W, BASIC, COBOL, Fortran, RPG, GNU Octave, MATLAB, PL/I, Scala, Visual Basic, Visual Basic (.NET), Xojo
$name(index)XPath/XQuery
name.(index)OCaml
name.[index]F#, OCaml
name/indexRebol, Red
name!indexHaskell
$name?indexXPath/XQuery
(vector-ref name index)Scheme
(aref name index)Common Lisp
(elt name index)ISLISP
name[[index]]Mathematica, Wolfram Language
nameat:indexSmalltalk
[nameobjectAtIndex:index]Objective-C (NSArray * only)
index{nameJ
name.item(index) or name @ indexEiffel

Slicing

The following list contains syntax examples of how a range of element of an array can be accessed.

In the following table:

  • first – the index of the first element in the slice
  • last – the index of the last element in the slice
  • end – one more than the index of last element in the slice
  • len – the length of the slice (= end - first)
  • step – the number of array elements in each (default 1)
FormatLanguages
name[first:last]ALGOL 68, Julia, Icon, Unicon
name[first+(⍳len)-⎕IO]APL
name[first:end:step]Python
name[first:end]Go
name[first..last]Pascal, Object Pascal, Delphi, Nim
$name[first..last]Windows PowerShell
@name[first..last]Perl
name[first..last] name[first...end] name[first,len]Ruby
copy/part skip name first lenRebol, Red
name(first..last)Ada
name(first:last)Fortran, GNU Octave, MATLAB
name[[first;;last;;step]]Mathematica, Wolfram Language
name[[first:last]]S-Lang
name.[first..step..last]F#
name.slice(first,end)Haxe, JavaScript, Scala
name.slice(first,len)CFML
array_slice(name,first,len)PHP
(subseq name first end)Common Lisp
(subseq name first end)ISLISP
Array.sub name first lenOCaml
[namesubarrayWithRange:NSMakeRange(first,len)]Objective-C (NSArray * only)
(first([+i.@(-~)end){nameJ
name[first..<end] name[first...last]Swift
name copyFrom: first to:last name copyFrom: first count:lenSmalltalk
name[first..end]D, C#
name[first..end] name[first..=last]Rust
name[first:end]Cobra
table.move(name, first, last, 1, {})Lua

Array system cross-reference list

LanguageDefault base indexSpecifiable index typeSpecifiable base indexBound checkMultidimensionalDynamically sizedVectorized operations
Adaindex typeyesyescheckedyesinitsome, others definable
ALGOL 681noyesvariesyesyesuser definable
APL1?0 or 1checkedyesyesyes
AWK1yes, implicitlynouncheckedyes, as delimited stringyes, rehashedno
BASIC0?nocheckednoinit?
C0nonouncheckedpartiallyinit, heapno
Ch0nonocheckedyes, also array of arrayinit, heapyes
C++0nonouncheckedyes, also array of arrayheapno
C#0nopartialcheckedyesheapyes (LINQ select)
CFML1nonocheckedyes, also array of arrayyesno
COBOL1nonocheckedarray of arraynosome intrinsics
Cobra0nonocheckedarray of arrayheap?
Common Lisp0?nocheckedyesyesyes (map or map-into)
D0yesnovariesyesyes?
F#0nopartialcheckedyesheapyes (map)
FreeBASIC0noyescheckedyesinit, init?
Fortran1yesyesvariesyesyesyes
FoxPro1?nocheckedyesyes?
Go0nonocheckedarray of arraynono
Hack0yesyescheckedyesyesyes
Haskell0yesyescheckedyes, also array of arrayinit?
IDL0?nocheckedyesyesyes
ISLISP0?nocheckedyesinityes (map or map-into)
J0?nocheckedyesyesyes
Java0nonocheckedarray of arrayinit?
JavaScript0nonocheckedarray of arrayyesyes
Julia1yesyeschecked (can be skipped locally; or globally by user)yes, also array of arrayyesyes
Lingo1??uncheckedyesyesyes
Lua1?partialcheckedarray of arrayyes?
Mathematica1nonocheckedyesyesyes
MATLAB1?nocheckedyesyesyes
Nim0yesyesoptionalarray of arrayyesyes
Oberon0?nocheckedyesno?
Oberon-20?nocheckedyesyes?
Objective-C0nonocheckedarray of arrayyesno
OCaml0nonochecked by defaultarray of arrayinit?
Pascal, Object Pascalindex typeyesyesvariesyesvariessome
Perl0noyes ($[)checkedarray of arrayyesno
Raku0nonocheckedyesyesyes
PHP0yesyescheckedyesyesyes
PL/I1yesyesoptionalyesnoyes
Python0nonocheckedarray of arrayyesno
RPG1nono?nono?
R1?nouncheckedyes, also array of arrayyesyes
Ruby0nonocheckedarray of arrayyes?
Rust0nonocheckedarray of arrayno?
Sass1nonocheckedarray of arrayinit?
S-Lang0?nocheckedyesyesyes
Scala0nonocheckedarray of arrayinityes (map)
Scheme0?nocheckedarray of arrayinityes (map)
Smalltalk1?nocheckedarray of arrayyes?
Swift0nonocheckedarray of arrayyes?
Visual Basic (classic)0, 1, or index typenoyescheckedyesyes?
Visual Basic (.NET)0 or index typenopartialcheckedyesyesyes (LINQ select)
Wolfram Language1nonocheckedyesyesyes
Windows PowerShell0nonocheckedyesheap?
Xojo0nonocheckedyesyesno
XPath/XQuery1nonocheckedarray of arrayyesyes

Vectorized array operations

Some compiled languages such as Ada and Fortran, and some scripting languages such as IDL, MATLAB, and S-Lang, have native support for vectorized operations on arrays. For example, to perform an element by element sum of two arrays, a and b to produce a third c, it is only necessary to write

In addition to support for vectorized arithmetic and relational operations, these languages also vectorize common mathematical functions such as sine. For example, if x is an array, then

will result in an array y whose elements are sine of the corresponding elements of the array x.

Vectorized index operations are also supported. As an example,

is how one would use Fortran to create arrays from the even and odd entries of an array. Another common use of vectorized indices is a filtering operation. Consider a clipping operation of a sine wave where amplitudes larger than 0.5 are to be set to 0.5. Using S-Lang, this can be done by

Mathematical matrix operations

Language/ LibraryCreateDeterminantTransposeElementColumnRowEigenvalues
APLm←dims⍴x11 x12 ...-.×m⍉mm[i;j] or i j⌷mm[;j] or j⌷[2]m or j⌷⍤1⊢m or j⌷⍉mm[i;] or i⌷m⌹⍠1⊢m
Fortranm = RESHAPE([x11, x12, ...], SHAPE(m))TRANSPOSE(m)m(i,j)m(:,j)m(i,:)
Chm = {...}determinant(m)transpose(m)m[i-1][j-1]shape(m,0)shape(m,1)eigen(output, m, NULL)
Julia and its standard library LinearAlgebram = [1 2; 3 4] or m=[ 12 34 ]det(m)transpose(m) or m' for real matricesm[i, j]m[:, j]m[i, :]eigen(m).values
Mathematica / Wolfram Language{{x11, x12, ...}, ...}Det[m]Transpose[m]m[[i,j]]m[[;;,j]]m[[i]]Eigenvalues[m]
MATLAB / GNU Octavem = [...]det(m)m.'m(i,j)m(:,j)m(i,:)eig(m)
NumPym = mat(...)linalg.det(m)m.Tm[i-1,j-1]m[:,j-1]m[i-1,:]linalg.eigvals(m)
Rm <- matrix(...) or m <- array(...)det(m)t(m)m[i, j]m[, j]m[i, ]eigen(m)
S-Langm = reshape([x11, x12, ...], [new-dims])m = transpose(m)m[i,j]m[*,j]m[j,*]
SymPym = Matrix(...)m.det()m.Tm[i-1,j-1]m.col(j-1)m.row(i-1)m.eigenvals()