This article relies largely or entirely on a single source .(June 2013) |
This comparison of programming languages (array) compares the features of array data structures or matrix processing for various computer programming languages.
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.
Languages | Size | First | Last |
---|---|---|---|
Ada | name'Length | name'First | name'Last |
ALGOL 68 | UPB name - LWB name+1 2 UPB name - 2 LWB name+1 etc. | LWB name 2 LWB name etc. | UPB name 2 UPB name |
APL | ⍴ name (⍴ name)[index] | ⎕IO | (⍴ name)-~⎕IO (⍴ name)[index]-~⎕IO |
AWK | length | 1 | asorti |
C#, Visual Basic (.NET), Windows PowerShell, F# | name.Length | name.GetLowerBound(dimension) | name.GetUpperBound(dimension) |
CFML | arrayLen(name) name.len() | 1 | name.len() |
Ch | max(shape(name)) | 0 | max(shape(name))-1 |
Common Lisp | (length name) | 0 | (1- (length name)) |
D | name.length | 0 | name.length-1 $-1 |
Fortran | size(name) | lbound(name) | ubound(name) |
Go | len(name) | 0 | len(name) - 1 |
Haskell | rangeSize (bounds name) | fst (bounds name) | snd (bounds name) |
ISLISP | (length name) | 0 | (1- (length name)) |
Icon | *name | 1 | *name |
Cobra, D, Haxe, Java, JavaScript, Scala | name.length | 0 | name.length - 1 |
J | #name | 0 | <:@#name |
JavaScript (ES2022) | name.length | 0 name.at(0) [1] | name.length - 1 name.at(-1) [1] |
Julia | length(name) size(name) | begin first.(axes(name)) | end last.(axes(name)) |
Lingo | count(name) | 1 | getLast(name) |
LiveCode | length(name) | 1 first | -1 last |
Lua | #name | 1 by convention; any integer [2] | #name |
Mathematica | Length[name] | 1 First[name] | -1 Last[name] |
MATLAB, GNU Octave | length(name) | 1 | end |
Nim | name.len | name.low [3] | name.high |
Oberon | LEN(name) | 0 | LEN(name) - 1 |
Object Pascal | Length(name) | 0 low(name) | Length(name)-1 high(name) |
Objective-C (NSArray * only) | [name count] | 0 | [name count] - 1 |
OCaml | Array.length name | 0 | Array.length name - 1 |
Perl | scalar(@name) | $[ | $#name |
PHP | count(name) | 0 | count(name) - 1 |
PL/I | dim(name[,dim]) | lbound(name[,dim]) | hbound(name[,dim]) |
Python | len(name) | 0 | -1 len(name) - 1 |
R | length(name) | 1 | length(name) |
Raku | @name.elems | 0 | @name.end |
Red | length? name | name/1 first name | last name |
Ruby | name.size | 0 name.first | -1 name.size - 1 name.last |
Rust | name.len() | 0 | name.len() - 1 |
S-Lang | length(name) | 0 | -1 length(name)-1 |
Scheme | (vector-length vector) | 0 | (- (vector-length vector) 1) |
Smalltalk | name size | 1 name first | name size name last |
Swift | name.count | 0 | name.count - 1 |
Unicon | *name | 1 | *name |
Visual Basic | UBound(name)-LBound(name)+1 | LBound(name) | UBound(name) |
Wolfram Language | Length[name] | 1 First[name] | -1 Last[name] |
Xojo | UBound(name) | 0 | UBound(name) |
XPath/XQuery | count($name) | 1 | count($name) last() array:size(name) [4] |
The following list contains syntax examples of how to access a single element of an array.
Format | Languages |
---|---|
name[index] or name[index1, index2] etc. | ALGOL 58, ALGOL 60, ALGOL 68, AWK, Julia, Modula, Pascal, Object Pascal, C#, S-Lang [5] Icon, Unicon |
name[index] or name[index1; index2] etc. or index⌷name or index1 index2⌷name etc. | APL |
name[index] | ActionScript, C, CFML, Ch, Cobra, C++, D, Go, Haxe, Java, JavaScript, Lingo, Lua, Nim, Objective-C (NSArray * ), Perl, [5] PHP, Python, [5] R, Ruby, [5] Rust, Swift |
$name[index] | Perl, [5] Windows PowerShell, [5] XPath/XQuery [4] |
@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 [4] |
name.(index) | OCaml |
name.[index] | F#, OCaml |
name/index | Red |
name ! index | Haskell |
$name ? index | XPath/XQuery [4] |
(vector-ref name index) | Scheme |
(aref name index) | Common Lisp |
(elt name index) | ISLISP |
name[[index]] | Mathematica, [5] Wolfram Language |
name at:index | Smalltalk |
[name objectAtIndex:index] | Objective-C (NSArray * only) |
index{name | J |
name.item(index) or name @ index [6] | Eiffel |
The following list contains syntax examples of how a range of element of an array can be accessed.
In the following table:
Format | Languages |
---|---|
name[first:last] | ALGOL 68, [7] Julia, Icon, Unicon |
name[first+(⍳len)-⎕IO] | APL |
name[first:end:step] | Python [8] [9] |
name[first:end] | Go |
name[first..last] | Pascal, Object Pascal, Delphi, Nim |
$name[first..last] | Windows PowerShell |
@name[first..last] | Perl [10] |
name[first..last] name[first...end] name[first, len] | Ruby [9] |
copy/part skip name first len | Red |
name(first..last) | Ada [7] |
name(first:last) | Fortran, [7] [8] GNU Octave, MATLAB [7] [10] |
name[[first;;last;;step]] | Mathematica, [7] [8] [9] Wolfram Language |
name[[first:last]] | S-Lang [7] [8] [10] |
name.[first..step..last] | F# |
name.slice(first, end) | Haxe, JavaScript, Scala |
name.slice(first, len) | CFML |
array_slice(name, first, len) | PHP [9] |
(subseq name first end) | Common Lisp |
(subseq name first end) | ISLISP |
Array.sub name first len | OCaml |
[name subarrayWithRange:NSMakeRange(first, len)] | Objective-C (NSArray * only) |
(first([+i.@(-~)end){name | J |
name[first..<end] name[first...last] | Swift |
name copyFrom: first to:last name copyFrom: first count:len | Smalltalk |
name[first..end] | D, C# [11] [12] |
name[first..end] name[first..=last] | Rust |
name[first:end] | Cobra |
table.move(name, first, last, 1, {}) | Lua [13] |
Language | Default base index | Specifiable index type [14] | Specifiable base index | Bound check | Multidimensional | Dynamically-sized | Vectorized operations |
---|---|---|---|---|---|---|---|
Ada | index type [15] | yes | yes | checked | yes | init [16] | some, others definable [17] |
ALGOL 68 | 1 | no [18] | yes | varies | yes | yes | user definable |
APL | 1 | ? | 0 or 1 [19] | checked | yes | yes | yes |
AWK | 1 | yes, implicitly | no | unchecked | yes, as delimited string | yes, rehashed | no |
BASIC | 0 | ? | no | checked | no | init [16] | ? |
C | 0 | no | no [20] | unchecked | partially | init, [16] [21] heap [22] | no |
Ch | 0 | no | no | checked | yes, also array of array [23] | init, [16] [21] heap [22] | yes |
C++ [17] | 0 | no | no [20] | unchecked | yes, also array of array [23] | heap [22] | no |
C# | 0 | no | partial [24] | checked | yes | heap [22] [25] | yes (LINQ select) |
CFML | 1 | no | no | checked | yes, also array of array [23] | yes | no |
COBOL | 1 | no [26] | no | checked | array of array [23] [27] | no [28] | some intrinsics |
Cobra | 0 | no | no | checked | array of array [23] | heap | ? |
Common Lisp | 0 | ? | no | checked [29] | yes | yes | yes (map or map-into) |
D | 0 | yes [30] | no | varies [31] | yes | yes | ? |
F# | 0 | no | partial [24] | checked | yes | heap [22] [25] | yes (map) |
FreeBASIC | 0 | no | yes | checked | yes | init, [16] init [32] | ? |
Fortran | 1 | yes | yes | varies [33] | yes | yes | yes |
FoxPro | 1 | ? | no | checked | yes | yes | ? |
Go | 0 | no | no | checked | array of array [23] | no [34] | no |
Hack | 0 | yes | yes | checked | yes | yes | yes |
Haskell | 0 | yes [35] | yes | checked | yes, also array of array [23] | init [16] | ? |
IDL | 0 | ? | no | checked | yes | yes | yes |
ISLISP | 0 | ? | no | checked | yes | init [16] | yes (map or map-into) |
J | 0 | ? | no | checked | yes | yes | yes |
Java [17] | 0 | no | no | checked | array of array [23] | init [16] | ? |
JavaScript | 0 | no | no | checked [36] | array of array [23] | yes | yes |
Julia | 1 | yes | yes | checked (can be skipped locally; or globally by user) | yes, also array of array | yes | yes |
Lingo | 1 | ? | ? | unchecked | yes | yes | yes |
Lua | 1 | ? | partial [37] | checked | array of array [23] | yes | ? |
Mathematica | 1 | no | no | checked | yes | yes | yes |
MATLAB | 1 | ? | no | checked | yes [38] | yes | yes |
Nim | 0 | yes [3] | yes | optional [39] | array of array [23] | yes | yes [40] |
Oberon | 0 | ? | no | checked | yes | no | ? |
Oberon-2 | 0 | ? | no | checked | yes | yes | ? |
Objective-C [17] | 0 | no | no | checked | array of array [23] | yes | no |
OCaml | 0 | no | no | checked by default | array of array [23] | init [16] | ? |
Pascal, Object Pascal | index type [15] | yes | yes | varies [41] | yes | varies [42] | some |
Perl | 0 | no | yes ($[ ) | checked [36] | array of array [23] | yes | no [43] |
Raku | 0 | no | no | checked [36] | yes | yes | yes |
PHP | 0 | yes [44] | yes [44] | checked [44] | yes | yes | yes |
PL/I | 1 | yes | yes | optional | yes | no | yes [45] |
Python | 0 | no | no | checked | array of array [23] | yes | no [46] |
RPG | 1 | no | no | ? | no | no | ? |
R | 1 | ? | no | unchecked | yes, also array of array | yes | yes |
Ring | 1 | ? | partial [37] | checked | array of array [23] | yes | ? |
Ruby | 0 | no | no | checked [36] | array of array [23] | yes | ? |
Rust | 0 | no | no | checked | array of array [23] | no | ? |
Sass | 1 | no | no | checked | array of array [23] | init [31] | ? |
S-Lang | 0 | ? | no | checked | yes | yes | yes |
Scala | 0 | no | no | checked | array of array [23] | init [16] | yes (map) |
Scheme | 0 | ? | no | checked | array of array [23] | init [16] | yes (map) |
Smalltalk [17] | 1 | ? | no | checked | array of array [23] | yes [47] | ? |
Swift | 0 | no | no | checked | array of array [23] | yes | ? |
Visual Basic | 0, 1, or index type [48] | no | yes | checked | yes | yes | ? |
Visual Basic (.NET) | 0 or index type [48] | no | partial [24] | checked | yes | yes | yes (LINQ select) |
Wolfram Language | 1 | no | no | checked | yes | yes | yes |
Windows PowerShell | 0 | no | no | checked | yes | heap | ? |
Xojo | 0 | no | no | checked | yes | yes | no |
XPath/XQuery | 1 | no | no | checked | array of array [4] [23] | yes | yes |
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
c = a + b
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
y = sin (x)
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,
even=x(2::2);odd=x(::2);
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
y = sin(x); y[where(abs(y)>0.5)] = 0.5;
Language/ Library | Create | Determinant | Transpose | Element | Column | Row | Eigenvalues |
---|---|---|---|---|---|---|---|
APL | m←dims⍴x11x12... | -.×m | ⍉m | m[i;j] or ij⌷m | m[;j] or j⌷[2]m orj⌷⍤1⊢m or j⌷⍉m | m[i;] or i⌷m | ⌹⍠1⊢m |
Fortran | m = RESHAPE([x11, x12, ...], SHAPE(m)) | TRANSPOSE(m) | m(i,j) | m(:,j) | m(i,:) | ||
Ch [49] | m = {... } | determinant(m) | transpose(m) | m[i-1][j-1] | shape(m,0) | shape(m,1) | eigen(output, m, NULL) |
Julia and its standard libraryLinearAlgebra | m=[12;34] or m=[1234] | det(m) | transpose(m) or
| m[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 Octave | m = [...] | det(m) | m.' | m(i,j) | m(:,j) | m(i,:) | eig(m) |
NumPy | m = mat(...) | linalg.det(m) | m.T | m[i-1,j-1] | m[:,j-1] | m[i-1,:] | linalg.eigvals(m) |
R | m <- matrix(...) or m <- array(...) | det(m) | t(m) | m[i, j] | m[, j] | m[i, ] | eigen(m) |
S-Lang | m = reshape([x11, x12, ...], [new-dims]) | m = transpose(m) | m[i,j] | m[*,j] | m[j,*] | ||
SymPy | m = Matrix(...) | m.det() | m.T | m[i-1,j-1] | m.col(j-1) | m.row(i-1) | m.eigenvals() |
In computer science, an array is a data structure consisting of a collection of elements, of same memory size, each identified by at least one array index or key. An array is stored such that the position of each element can be computed from its index tuple by a mathematical formula. The simplest type of data structure is a linear array, also called one-dimensional array.
C is a general-purpose programming language. It was created in the 1970s by Dennis Ritchie and remains very widely used and influential. By design, C's features cleanly reflect the capabilities of the targeted CPUs. It has found lasting use in operating systems code, device drivers, and protocol stacks, but its use in application software has been decreasing. C is commonly used on computer architectures that range from the largest supercomputers to the smallest microcontrollers and embedded systems.
In computer science, a data structure is a data organization, and storage format that is usually chosen for efficient access to data. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data, i.e., it is an algebraic structure about data.
Euphoria is a programming language created by Robert Craig of Rapid Deployment Software in Toronto, Ontario, Canada. Initially developed on the Atari ST, the first commercial release was for MS-DOS as proprietary software. In 2006, with the release of version 3, Euphoria became open-source software. The openEuphoria Group continues to administer and develop the project. In December 2010, the openEuphoria Group released version 4 of openEuphoria along with a new identity and mascot for the project. OpenEuphoria is currently available for Windows, Linux, macOS and three flavors of *BSD.
Pascal is an imperative and procedural programming language, designed by Niklaus Wirth as a small, efficient language intended to encourage good programming practices using structured programming and data structuring. It is named after French mathematician, philosopher and physicist Blaise Pascal.
Lua is a lightweight, high-level, multi-paradigm programming language designed mainly for embedded use in applications. Lua is cross-platform software, since the interpreter of compiled bytecode is written in ANSI C, and Lua has a relatively simple C application programming interface (API) to embed it into applications.
In computer science and computer programming, a data type is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these values as machine types. A data type specification in a program constrains the possible values that an expression, such as a variable or a function call, might take. On literal data, it tells the compiler or interpreter how the programmer intends to use the data. Most programming languages support basic data types of integer numbers, floating-point numbers, characters and Booleans.
Generic programming is a style of computer programming in which algorithms are written in terms of data types to-be-specified-later that are then instantiated when needed for specific types provided as parameters. This approach, pioneered by the ML programming language in 1973, permits writing common functions or types that differ only in the set of types on which they operate when used, thus reducing duplicate code.
In mathematics and computer programming, index notation is used to specify the elements of an array of numbers. The formalism of how indices are used varies according to the subject. In particular, there are different methods for referring to the elements of a list, a vector, or a matrix, depending on whether one is writing a formal mathematical paper for publication, or when one is writing a computer program.
The syntax of the C programming language is the set of rules governing writing of software in C. It is designed to allow for programs that are extremely terse, have a close relationship with the resulting object code, and yet provide relatively high-level data abstraction. C was the first widely successful high-level language for portable operating-system development.
In computer science, a pointer is an object in many programming languages that stores a memory address. This can be that of another value located in computer memory, or in some cases, that of memory-mapped computer hardware. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer. As an analogy, a page number in a book's index could be considered a pointer to the corresponding page; dereferencing such a pointer would be done by flipping to the page with the given page number and reading the text found on that page. The actual format and content of a pointer variable is dependent on the underlying computer architecture.
IDL, short for Interactive Data Language, is a programming language used for data analysis. It is popular in particular areas of science, such as astronomy, atmospheric physics and medical imaging. IDL shares a common syntax with PV-Wave and originated from the same codebase, though the languages have subsequently diverged in detail. There are also free or costless implementations, such as GNU Data Language (GDL) and Fawlty Language (FL).
In computer programming, foreach loop is a control flow statement for traversing items in a collection. foreach is usually used in place of a standard for loop statement. Unlike other for loop constructs, however, foreach loops usually maintain no explicit counter: they essentially say "do this to everything in this set", rather than "do this x times". This avoids potential off-by-one errors and makes code simpler to read. In object-oriented languages, an iterator, even if implicit, is often used as the means of traversal.
In computer programming, array slicing is an operation that extracts a subset of elements from an array and packages them as another array, possibly in a different dimension from the original.
A bit array is an array data structure that compactly stores bits. It can be used to implement a simple set data structure. A bit array is effective at exploiting bit-level parallelism in hardware to perform operations quickly. A typical bit array stores kw bits, where w is the number of bits in the unit of storage, such as a byte or word, and k is some nonnegative integer. If w does not divide the number of bits to be stored, some space is wasted due to internal fragmentation.
The computer programming languages C and Pascal have similar times of origin, influences, and purposes. Both were used to design their own compilers early in their lifetimes. The original Pascal definition appeared in 1969 and a first compiler in 1970. The first version of C appeared in 1972.
S-algol is a computer programming language derivative of ALGOL 60 developed at the University of St Andrews in 1979 by Ron Morrison and Tony Davie. The language is a modification of ALGOL to contain orthogonal data types that Morrison created for his PhD thesis. Morrison would go on to become professor at the university and head of the department of computer science. The S-algol language was used for teaching at the university at an undergraduate level until 1999. It was also the language taught for several years in the 1980s at a local school in St. Andrews, Madras College. The computer science text Recursive Descent Compiling describes a recursive descent compiler for S-algol, implemented in S-algol.
In computer programming, an enumerated type is a data type consisting of a set of named values called elements, members, enumeral, or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language. An enumerated type can be seen as a degenerate tagged union of unit type. A variable that has been declared as having an enumerated type can be assigned any of the enumerators as a value. In other words, an enumerated type has values that are different from each other, and that can be compared and assigned, but are not specified by the programmer as having any particular concrete representation in the computer's memory; compilers and interpreters can represent them arbitrarily.
This article compares a large number of programming languages by tabulating their data types, their expression, statement, and declaration syntax, and some common operating-system interfaces.
In computer science, array is a data type that represents a collection of elements, each selected by one or more indices that can be computed at run time during program execution. Such a collection is usually called an array variable or array value. By analogy with the mathematical concepts vector and matrix, array types with one and two indices are often called vector type and matrix type, respectively. More generally, a multidimensional array type can be called a tensor type, by analogy with the physical concept, tensor.
first:last:step
are also supported.array[indices]
, where indices
can be a range such mentioned in footnote 2 or an explicit list of indices, e.g., '[0,9,3,4]
', or a mix of both, e.g., A[[[0:3]],7,9,[11:2:-3]]]
.list
, does not support vectorized operations as defined here. However, the numpy extension adds array objects with this ability