Comparison of programming languages (array)
Syntax
Array dimensions
The following list contains Syntax examples on how to determine the dimensions (index of the first element, the last element and or the size in elements):
Size | First | Last | Languages |
---|---|---|---|
name'Length | name'First | name'Last | Ada |
UPB name - LWB name+1 or 2 UPB name - 2 LWB name+1 etc |
LWB name or 2 LWB name etc | UPB name or 2 UPB name etc | ALGOL 68 |
UBound(name)-LBound(name)+1 | LBound(name) | UBound(name) | Visual Basic |
name.Length | name.GetLowerBound(dimension) | name.GetUpperBound(dimension) | C#, Visual Basic .NET, Windows PowerShell, F# |
name.length | 0 | name.length - 1 | Java, JavaScript, D |
scalar(@name) | $[ | $#name | Perl |
@name.elems | 0 | @name.end | Perl 6 |
name.size | 0 (name.first will also refer to this element) | -1 or name.size - 1 (name.last will also refer to this element) | Ruby |
len(name) | 0 | -1 or len(name) - 1 | Python |
length(name) | 0 | -1 or length(name)-1 | S-Lang |
(vector-length vector) | 0 | (- (vector-length vector) 1) | Scheme |
(length name) | 0 | (1- (length name)) | Common Lisp |
Array.length name | 0 | Array.length name - 1 | OCaml |
rangeSize (bounds name) | fst (bounds name) | snd (bounds name) | Haskell |
[name count] | 0 | [name count] - 1 | Objective-C (NSArray * only)
|
The following list contains Syntax examples on how a single element of an array can be accessed.
Format | Languages |
---|---|
name[index] or name[index1, index2] etc | ALGOL 68, Pascal, C# |
name[index] | C, C++, D, Java, JavaScript, Perl1, PHP, Python1, Ruby1, S-Lang1 |
$name[index] | Perl1, PHP, Windows PowerShell1 |
@name[index] | Perl 6 |
name(index) | Ada, Fortran, Visual Basic, Visual Basic .NET |
name.(index) | OCaml |
name.[index] | F# |
name ! index | Haskell |
(vector-ref name index) | Scheme |
(aref name index) | Common Lisp |
[name objectAtIndex:index] | Objective-C (NSArray * only)
|
- The index may be a negative number, indicating the corresponding number of places before the end of the array.
The following list contains Syntax examples on 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)
Format | Languages |
---|---|
name[first:last] | ALGOL 681 |
name[first:end] | Python2,4 |
name[first..last] | D |
$name[first..last] | Windows PowerShell |
@name[first..last] | Perl3 |
name[first..last] name[first...end] name[first, len] |
Ruby (programming language)4 |
name(first..last) | Ada1 |
name(first:last) | Fortran1,2 |
name'''first''':'''last''' | S-Lang1,2,3 |
name.[first..last] | F# |
name.slice(first, last) | JavaScript |
array_slice(name, first, len) | PHP4 |
(subseq name first end) | Common Lisp |
Array.sub name first len | OCaml |
[name subarrayWithRange:NSMakeRange(first, len)] | Objective-C (NSArray * only)
|
- Slices for multidimensional arrays are also supported and defined similarly.
- Slices of the type
first:last:step
are also supported. - More generally, for 1-d arrays Perl and S-Lang permit slices of the form
array[indices]
, whereindices
an be a range such mentioned in footnote 2 or an explicit list of indices, e.g., '[0,9,3,4]
', as well as a combination of the two, e.g.,A[[[0:3],7,9,[11:2:-3]]]
. - last or end may be a negative number, indicating to stop at the corresponding number of places before the end of the array.
Array system cross-reference list
Programming language | Default Base index | Specifiable Index Type16 | Specifiable Base Index | Bound Check | Multidimensional | Dynamically-sized | Vectorized Operations |
---|---|---|---|---|---|---|---|
Ada | index type17 | yes | yes | checked | yes | init1 | some, others definable5 |
ALGOL 68 | 1 | no25 | yes | varies | yes | yes | user definable |
APL | ? | ? | 0 or 17 | checked | yes | init1 | yes |
assembly language | 0 | ? | no | unchecked | no | no | ? |
BASIC | 0 | ? | no | checked | no | init1 | ? |
C | 0 | no | no | unchecked | array of array2 | init1,4, heap3 | no |
C++5 | 0 | no | no | unchecked | array of array2 | heap3 | no |
C# | 0 | no | no | checked | yes | heap3,9 | yes (LINQ select) |
COBOL | 1 | ? | no | checked | yes | no14 | ? |
Common Lisp | 0 | ? | no | checked15 | yes | yes | yes (mapcar) |
D | 0 | ? | no | varies11 | yes | yes | ? |
F# | 0 | no | no | checked | yes | heap3,9 | no |
FreeBASIC | 0 | no | yes | checked | yes | init1,init21 | ? |
Fortran | 1 | yes | yes | varies12 | yes | init1, heap3 | yes |
FoxPro | 1 | ? | no | checked | yes | yes | ? |
Haskell | none (specified on init) | yes24 | yes | checked | yes, also array of array2 | init1 | ? |
IDL | 0 | ? | no | checked | yes | yes | yes |
Java5 | 0 | no | no | checked | array of array2 | init1 | ? |
JavaScript | 0 | no | no | checked22 | array of array2 | yes | yes |
Lua | 1 | ? | partial20 | checked | array of array2 | yes | ? |
MATLAB | 1 | ? | no | checked | yes8 | yes | yes |
Oberon | 0 | ? | no | checked | yes | no | ? |
Oberon-2 | 0 | ? | no | checked | yes | yes | ? |
Objective-C5 | 0 | no | no | checked | array of array2 | yes | no |
OCaml | 0 | no | no | checked by default | array of array2 | init1 | ? |
Pascal | index type17 | yes | yes | varies13 | yes | varies10 | some |
Perl | 0 | no | yes ($[ )
|
checked22 | array of array2 | yes | no18 |
PHP | 0 | yes23 | yes23 | checked23 | yes | yes | yes |
PL/I | 1 | ? | yes | checked | yes | no | ? |
Python | 0 | no | no | checked | array of array2 | yes | no19 |
Ruby | 0 | no | no | checked22 | array of array2 | yes | ? |
S-Lang | 0 | ? | no | checked | yes | yes | yes |
Scheme | 0 | ? | no | checked | array of array2 | init1 | ? |
Smalltalk5 | 1 | ? | no | checked | array of array2 | yes6 | ? |
Visual Basic | 0 | no | yes | checked | yes | yes | ? |
Visual Basic .NET | 0 | no | no | checked | yes | yes | yes (LINQ select) |
Windows PowerShell | 0 | no | no | checked | yes | heap | ? |
Programming language | Default Base index | Specifiable Index Type16 | Specifiable Base Index | Bound Check | Multidimensional | Dynamically-sized | Vectorized Operations |
- Size can only be chosen on initialization after which it is fixed.
- Allows arrays of arrays which can be used to emulate most - but not all - aspects multi-dimensional arrays.
- Size can only be chosen on initialization when memory is allocated on the heap, as distinguished from when it is allocated on the stack. This note need not be made for a language that always allocates arrays on the heap.
- C99 allows for variable size arrays – however there is almost no compiler available to support this new feature.
- This list is strictly comparing language features. In every language (even assembler) it is possible to provide improved array handling via add on libraries. This language has improved array handling as part of its standard library.
- The class Array is fixed-size, but OrderedCollection is dynamic.
- The indexing base can be 0 or 1, but is set for a whole "workspace".
- At least 2 dimensions (scalar numbers are 1×1 arrays, vectors are 1×n or n×1 arrays).
- Allows creation of fixed-size arrays in "unsafe" code, allowing for enhanced interoperability with other languages
- Varies by implementation. Newer implementations (FreePascal and Delphi) permit heap-based dynamic arrays.
- Behaviour can be tuned using compiler switches. As in DMD 1.0 bounds are checked in debug mode and unchecked in release mode for efficiency reasons.
- Almost all Fortran implementations offer bounds checking options via compiler switches. However by default, bounds checking is usually turned off for efficiency reasons.
- Many implementations (Turbo Pascal, Delphi, FreePascal) allow the behaviour to be changed by compiler switches and in-line directives.
- COBOL provides a way to specify that the usable size of an array is variable, but this can never be greater than the declared maximum size, which is also the allocated size.
- Most Common Lisp implementations allow checking to be selectively disabled.
- The index type can be a freely chosen Integer type, Enumerated type, or Character type. For arrays with non-compact index types see: Associative array.
- The default base index is the lowest value of the index type used.
- Standard Perl array data types do not support vectorized operations as defined here. However, the PerlDL extension adds array objects with this capability.
- The standard Python array type,
list
, does not support vectorized operations as defined here. However, the numpy extension adds array objects with this capability. - By specifying a base index, arrays at an arbitrary base can be created. However, by default, Lua's length operator does not consider the base index of the array when calculating the length. This behavior can be changed via metamethods.
- FreeBASIC supports both variable array lengths and fixed length arrays. Arrays declared with no index range are created as variable-length arrays, while arrays with a declared range are created as fixed-length arrays.
- In this language, you can access or write to an array index greater than or equal to the length of the array, and the array will implicitly grow to that size. This may appear at first as if the bounds are not checked; however, the bounds are checked in order to decide to grow the array, and you do not have unsafe memory access like you do in C.
- PHP's "arrays" are associative arrays. You can use integers and strings as the keys (indexes); floats can also be used as the key but are truncated to integers. There is not really any "base index" or "bounds".
- Haskell arrays (Data.Array) allow you to use any type which is an instance of Ix as index type. So you can define a custom type and use it as an index type as long as it instances Ix. Also, tuples of Ix types are also Ix types; this is commonly used to implement multi-dimensional arrays.
- ALGOL 68 arrays must be subscripted (and sliced) by type INT. However a hash function could be used to convert other types to INT. eg. name[hash("string")]
Vectorized array operations
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
Since the implicit inner loops do not occur at the level of the interpreter, the speed of such vectorized operations can be as fast as compiled code. 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[[0::2]]; odd = x[[1::2]];
is how one would use S-Lang create arrays from the even and odd elements 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 may accomplished by
y = sin(x); y[where(abs(y)>0.5)] = 0.5;