Comparison of programming languages (array)

From Wikipedia, the free encyclopedia

This article is part of the
Programming Language Comparison
series.
General Comparison
Basic Syntax
Basic Instructions
Arrays
Associative arrays
String Operations
String Functions
Object-oriented programming
Database access

Evaluation strategy
List of "hello world" programs

Comparison of ALGOL 68 and C++
Compatibility of C and C++
Comparison of C and Pascal
Comparison of C++ and Java
Comparison of C# and Java
Comparison of C# and Visual Basic .NET
Comparison of ABAP and Java
This box: view  talk  edit


Contents

[edit] Syntax

[edit] 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
array'Length array'First array'Last Ada
array.Length array.GetLowerBound(dimension) array.GetUpperBound(dimension) C#, Visual Basic .NET, Windows PowerShell
array.length 0 array.length - 1 Java, JavaScript, D
scalar(@array) 0 $#array Perl

[edit] Indexing

The following list contains Syntax examples on how a single element of an array can be accessed.

Format Languages
array[i] C, C++, D, C#, Java, JavaScript, Pascal, Python, Perl, PHP, Ruby, S-Lang
$array[i] Perl, PHP, Windows PowerShell
@array[i] Perl 6
array (i) Ada, Fortran, Visual Basic, Visual Basic .NET
array.(i) OCaml
(vector-ref vector i) Scheme

[edit] Slicing

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

Format Languages
array[first..last] D
$array[first..last] Windows PowerShell
@array[first..last] Perl
array (first .. last) Ada1
array(first:last) Fortran1,2
array[[first:last]] S-Lang1,2,3
  1. Slices for multidimensional arrays are also supported and defined similarly.
  2. Slices of the type first:last:step are also supported.
  3. More generally, for 1-d arrays S-Lang permits slices of the form array[indices], where indices 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]]].

[edit] 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
APL  ?  ? 0 or 17 checked yes init1 yes
ALGOL 68 1  ? yes varies yes yes user definable
assembly language 0  ? no unchecked no no  ?
BASIC 1  ? no checked no init1  ?
C 0 no no unchecked yes, also array of array2 init1, heap3,4  ?
C++5 0 no no unchecked yes, also array of array2 init1, heap3  ?
C# 0  ? no checked yes, also array of array2 heap3,9  ?
COBOL 1  ? no checked yes no14  ?
Common Lisp 0  ? no checked15 yes yes yes (mapcar)
D 0  ? no varies11 yes yes  ?
FreeBASIC 1  ? yes checked yes yes  ?
Fortran 1  ? yes varies12 yes init1,heap3  ?
FoxPro 1  ? no checked yes yes  ?
Haskell  ?  ? yes checked array of array2 yes  ?
IDL 0  ? no checked yes yes yes
Java5 0  ? no checked array of array2 heap3  ?
JavaScript 0  ?  ? checked array of array2 yes yes
Lua 1  ? partial19 checked array of array2 yes  ?
MATLAB 1  ? no checked yes8 yes yes
Oberon-1 0  ? no checked yes no  ?
Oberon-2 0  ? no checked yes yes  ?
OCaml 0  ? no checked array of array2 heap3  ?
Pascal index type17 yes yes varies13 yes varies10 some
Perl 0  ? yes checked array of array2 yes  ?
PHP 0  ?  ?  ? yes yes yes
PL/I  ?  ? yes checked  ?  ?  ?
Python 0  ? no checked array of array2 yes no18
Ruby 0  ? no checked array of array2 yes  ?
S-Lang 0  ? no checked yes yes yes
Scheme 0  ? no checked array of array2 no  ?
Smalltalk5 1  ? no checked array of array2 yes6  ?
Visual Basic 1  ? yes checked yes yes  ?
Visual Basic .NET 0  ? no checked yes yes  ?
Windows PowerShell 0  ? no checked array of array2 heap  ?
Programming language Default Base index Specifiable Index Type16 Specifiable Base Index Bound Check Multidimensional Dynamically-sized Vectorized Operations
  1. Size can be chosen on initialization/declaration after which it is fixed.
  2. Allows arrays of arrays which can be used to emulate most - but not all - aspects multi-dimensional arrays.
  3. Size can only be chosen when memory is allocated on the heap.
  4. C99 allows for variable size arrays – however there is almost no compiler available to support this new feature.
  5. 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.
  6. The class Array is fixed-size, but OrderedCollection is dynamic.
  7. The indexing base can be 0 or 1, but is set for a whole "workspace".
  8. At least 2 dimensions (scalar numbers are 1×1 arrays, vectors are 1×n or n×1 arrays).
  9. Allows creation of fixed-size arrays in "unsafe" code, allowing for enhanced interoperability with other languages
  10. Varies by implementation. Newer implementations (FreePascal and Delphi) permit heap-based dynamic arrays.
  11. 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.
  12. Almost all Fortran implementations offer bounds checking options via compiler switches. However by default, bounds checking is usually turned off for efficiency reasons.
  13. Many implementations (Turbo Pascal, Delphi, FreePascal) allow the behaviour to be changed by compiler switches and in-line directives.
  14. 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.
  15. Most Common Lisp implementations allow checking to be selectively disabled.
  16. 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.
  17. The default base index is the lowest value of the index type used.
  18. Native Python does not support vectorized operations as defined here. However, there do exist third-party extensions such as numarray, Numeric, and numpy that do add array objects with this capability.
  19. 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.

[edit] 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 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;