Retrieving an Element of an Array: Difference between revisions

From Rosetta Code
Content added Content deleted
(tag obsolete (Arrays) and remove {header}s)
Line 1: Line 1:
{{task|Basic language learning}}In this task, the goal is to retrieve an element of an [[array]].
{{task|Basic language learning}}


'''This task is obsolete. Please do not add new code, and merge existing code to the [[Arrays]] task.'''
=={{header|4D}}==

In this task, the goal is to retrieve an element of an [[array]].

==[[4D]]==
` first element
` first element
$elem:=$array{1}
$elem:=$array{1}


=={{header|ActionScript}}==
==[[ActionScript]]==
<lang actionscript>
<lang actionscript>
var arr:Array = new Array(1,2,3);
var arr:Array = new Array(1,2,3);
Line 12: Line 16:
</lang>
</lang>


=={{header|Ada}}==
==[[Ada]]==
Array indexed by an enumerated type. Ada enumerated types are discrete non-numeric types.
Array indexed by an enumerated type. Ada enumerated types are discrete non-numeric types.
<lang ada>
<lang ada>
Line 23: Line 27:
Monday_Sales is assigned 200
Monday_Sales is assigned 200


=={{header|ALGOL 68}}==
==[[ALGOL 68]]==
{{trans|FORTRAN}}
{{trans|FORTRAN}}


Line 66: Line 70:
</pre>
</pre>


=={{header|AppleScript}}==
==[[AppleScript]]==
on getArrayValue(array, location)
on getArrayValue(array, location)
-- very important -- The list index starts at 1 not 0
-- very important -- The list index starts at 1 not 0
Line 72: Line 76:
end getArrayValue
end getArrayValue


=={{header|AutoHotkey}}==
==[[AutoHotkey]]==
Arrays use one-based indexing. Array0 contains the number of elements.
Arrays use one-based indexing. Array0 contains the number of elements.
<lang autohotkey>string = abcde
<lang autohotkey>string = abcde
Line 79: Line 83:
MsgBox, % array%A_Index%</lang>
MsgBox, % array%A_Index%</lang>


=={{header|AWK}}==
==[[AWK]]==
This shows how a string is split into elements of the array a, which is iterated over, and its elements printed to stdout. Note that the order is not as original, because the array is implemented as a hash table.
This shows how a string is split into elements of the array a, which is iterated over, and its elements printed to stdout. Note that the order is not as original, because the array is implemented as a hash table.
$ awk 'BEGIN{split("a b c d",a);for(i in a)print a[i]}'
$ awk 'BEGIN{split("a b c d",a);for(i in a)print a[i]}'
Line 87: Line 91:
c
c


=={{header|C}}==
==[[C]]==
<lang c> int array_index(int array[], int index) {
<lang c> int array_index(int array[], int index) {
return array[index];
return array[index];
}</lang>
}</lang>


=={{header|C sharp|C#}}==
==[[C sharp|C#]]==
int getArrayValue( int values[], int index ) {
int getArrayValue( int values[], int index ) {
return values[index];
return values[index];
}
}


=={{header|C++}}==
==[[C++]]==


<lang cpp> template<typename T>
<lang cpp> template<typename T>
Line 104: Line 108:
}</lang>
}</lang>


=={{header|ColdFusion}}==
==[[ColdFusion]]==
<cfset arr = ArrayNew(1)>
<cfset arr = ArrayNew(1)>
<cfset arr[1] = "one">
<cfset arr[1] = "one">
Line 114: Line 118:
''ColdFusion Arrays are '''NOT''' zero-based, their index begins at '''1'''''
''ColdFusion Arrays are '''NOT''' zero-based, their index begins at '''1'''''


=={{header|Common Lisp}}==
==[[Common Lisp]]==
(defun array-value (array index)
(defun array-value (array index)
(aref array index))
(aref array index))


=={{header|D}}==
==[[D]]==
Generic, template-based method. Allows retriving elements
Generic, template-based method. Allows retriving elements
of arrays and objects having opIndex method implemented.
of arrays and objects having opIndex method implemented.
Line 165: Line 169:
</lang>
</lang>


=={{header|Delphi}}/{{header|Object Pascal}}/Standard {{header|Pascal}}==
==[[Delphi}}/[[Object Pascal}}/Standard [[Pascal]]==


Arrays in all the flavors of pascal can be of any valid base type, or user defined type (which are all made up of base types) and are multi-dimensional. With Delphi dynamic arrays were defined but had been used in pascal since its inception.
Arrays in all the flavors of pascal can be of any valid base type, or user defined type (which are all made up of base types) and are multi-dimensional. With Delphi dynamic arrays were defined but had been used in pascal since its inception.
Line 213: Line 217:
where n is the array index who's base is either 1 or 0 depending on how it was declared.
where n is the array index who's base is either 1 or 0 depending on how it was declared.


=={{header|E}}==
==[[E]]==


<lang e>def value := array[index]</lang>
<lang e>def value := array[index]</lang>


=={{header|Erlang}}==
==[[Erlang]]==
:''Array module work with arrays'':
:''Array module work with arrays'':


Value = array:get(Index, Array).
Value = array:get(Index, Array).


=={{header|Forth}}==
==[[Forth]]==
Forth does not have special syntax for array access. Address arithmetic is used to access contiguous memory.
Forth does not have special syntax for array access. Address arithmetic is used to access contiguous memory.
create array 1 , 2 , 3 , 4 ,
create array 1 , 2 , 3 , 4 ,
array 2 cells + @ . \ 3
array 2 cells + @ . \ 3


=={{header|Fortran}}==
==[[Fortran]]==
In ANSI Fortran 90 or later:
In ANSI Fortran 90 or later:
real, dimension(-10:20) :: a ! a one-dimensional real array indexed from -10 to 20
real, dimension(-10:20) :: a ! a one-dimensional real array indexed from -10 to 20
Line 251: Line 255:




=={{header|Groovy}}==
==[[Groovy]]==
Define an array
Define an array
arr = ['groovy', 'is', 'a', 'great', 'language']
arr = ['groovy', 'is', 'a', 'great', 'language']
Line 267: Line 271:
arr[0..2, -1] // *** ['groovy', 'is', 'a', 'language']
arr[0..2, -1] // *** ['groovy', 'is', 'a', 'language']


=={{header|Haskell}}==
==[[Haskell]]==


Arrays can have arbitrary bounds (not restricted to integer values, any instance of ''Ix'' will do):
Arrays can have arbitrary bounds (not restricted to integer values, any instance of ''Ix'' will do):
Line 279: Line 283:
Here, ''result'' will be equal to "an". It should be noted that in Haskell lists are often used instead of arrays.
Here, ''result'' will be equal to "an". It should be noted that in Haskell lists are often used instead of arrays.


=={{header|IDL}}==
==[[IDL]]==
; this is allowed:
; this is allowed:
result = arr(5)
result = arr(5)
Line 287: Line 291:
The form with square brackets is preferred as it unambiguously constitutes array access, while the version with round ones can conflict with a function call if there are both a function and an array with the same name <tt>arr</tt>.
The form with square brackets is preferred as it unambiguously constitutes array access, while the version with round ones can conflict with a function call if there are both a function and an array with the same name <tt>arr</tt>.


=={{header|J}}==
==[[J]]==


Arrays are the only way J handles data, so every program that produces a portion of a given array is relevant here. In these few examples emphasis is on the primary verb <tt>{</tt> ("from").
Arrays are the only way J handles data, so every program that produces a portion of a given array is relevant here. In these few examples emphasis is on the primary verb <tt>{</tt> ("from").
Line 323: Line 327:
30 31
30 31


=={{header|Java}}==
==[[Java]]==
Object element = array[index];
Object element = array[index];


=={{header|JavaScript}}==
==[[JavaScript]]==
var element = array[index];
var element = array[index];


=={{header|Logo}}==
==[[Logo]]==
print item 1 {10 20 30} ; 10
print item 1 {10 20 30} ; 10


=={{header|LSE64}}==
==[[LSE64]]==
10 array :array
10 array :array
array 5 [] @ # contents of sixth cell in array
array 5 [] @ # contents of sixth cell in array


=={{header|Mathematica}}==
==[[Mathematica]]==
<lang Mathematica>
<lang Mathematica>
element = array[[index]]
element = array[[index]]
Line 342: Line 346:
First index is 1, negative indices count from the back, -1 being the last element. [ [ 0 ] ] returns the head. The [ [ ] ]-command (Part) works not only on a list, it works on everything: graphics, equations, matrices of any dimension et cetera. To dig deeper you can specifiy multiple indices [ [ a, b, c ] ] to get the c<sup>th</sup> element of the b<sup>th</sup> element of the a<sup>th</sup> element. For any level you can specify a range using ;;, i.e.: a;;b;;c would give elements a through b in steps of c. If a is not given it will assume 1, if b is not give it will be -1, if c is not given it will be 1.
First index is 1, negative indices count from the back, -1 being the last element. [ [ 0 ] ] returns the head. The [ [ ] ]-command (Part) works not only on a list, it works on everything: graphics, equations, matrices of any dimension et cetera. To dig deeper you can specifiy multiple indices [ [ a, b, c ] ] to get the c<sup>th</sup> element of the b<sup>th</sup> element of the a<sup>th</sup> element. For any level you can specify a range using ;;, i.e.: a;;b;;c would give elements a through b in steps of c. If a is not given it will assume 1, if b is not give it will be -1, if c is not given it will be 1.


=={{header|MAXScript}}==
==[[MAXScript]]==
item = arr[index]
item = arr[index]


=={{header|mIRC Scripting Language}}==
==[[mIRC Scripting Language]]==
{{works with|mIRC Script Editor}}
{{works with|mIRC Script Editor}}
{{works with|mArray Snippet}}
{{works with|mArray Snippet}}
Line 352: Line 356:
alias readmyarray { echo -a $array_read(MyArray, 2, 3) }
alias readmyarray { echo -a $array_read(MyArray, 2, 3) }


=={{header|Modula-3}}==
==[[Modula-3]]==
<pre>
<pre>
VAR arr := ARRAY [1..3] OF INTEGER {1, 2, 3};
VAR arr := ARRAY [1..3] OF INTEGER {1, 2, 3};
Line 358: Line 362:
</pre>
</pre>


=={{header|Nial}}==
==[[Nial]]==
Nial is an array programming language. Thus it has a variety of ways
Nial is an array programming language. Thus it has a variety of ways
to retrieve elements of an (even multi-dimensional) array.
to retrieve elements of an (even multi-dimensional) array.
Line 381: Line 385:
There are quite a few others (too large to list here).
There are quite a few others (too large to list here).


=={{header|Objective-C}}==
==[[Objective-C]]==
{{works with|GNUstep}}
{{works with|GNUstep}}


Line 390: Line 394:
id element = [array objectAtIndex:index];</lang>
id element = [array objectAtIndex:index];</lang>


=={{header|OCaml}}==
==[[OCaml]]==
let element = array.(index)
let element = array.(index)


=={{header|Octave}}==
==[[Octave]]==


<lang octave>a = [1:10];
<lang octave>a = [1:10];
Line 403: Line 407:




=={{header|Perl}}==
==[[Perl]]==
{{works with|Perl|5.8.8}}
{{works with|Perl|5.8.8}}
$elem = $array[0];
$elem = $array[0];


=={{header|PHP}}==
==[[PHP]]==
<lang php> $array = array('php', 'has', 'arrays');
<lang php> $array = array('php', 'has', 'arrays');
// First element
// First element
$elem = $array[0];</lang>
$elem = $array[0];</lang>


=={{header|Pop11}}==
==[[Pop11]]==
lvars ar = {1 two 'three'};
lvars ar = {1 two 'three'};
lvars elem;
lvars elem;
Line 420: Line 424:
This example uses the simplest possible array (a vector). Pop11 has more general arrays, but in all cases access follows the same pattern, and look the same as procedure (function) call.
This example uses the simplest possible array (a vector). Pop11 has more general arrays, but in all cases access follows the same pattern, and look the same as procedure (function) call.


=={{header|Python}}==
==[[Python]]==
{{works with|Python|2.5}}
{{works with|Python|2.5}}
The item is an element in a list at a given index
The item is an element in a list at a given index
Line 432: Line 436:
'''Note:''' When using the pop() method, the element is removed from the list.
'''Note:''' When using the pop() method, the element is removed from the list.


=={{header|R}}==
==[[R]]==


An element from a vector can be retrieved with the []; slices can be taken using ranges or another vector of indexes.
An element from a vector can be retrieved with the []; slices can be taken using ranges or another vector of indexes.
Line 445: Line 449:
print(a[2,1])</lang>
print(a[2,1])</lang>


=={{header|Ruby}}==
==[[Ruby]]==
<lang ruby> ary = ['Ruby','rules','big','time']
<lang ruby> ary = ['Ruby','rules','big','time']
#the first element
#the first element
Line 467: Line 471:
# => element = "big"</lang>
# => element = "big"</lang>


=={{header|Scheme}}==
==[[Scheme]]==
Lists are more often used in Scheme than vectors
Lists are more often used in Scheme than vectors
<lang scheme> (vector-ref array index)</lang>
<lang scheme> (vector-ref array index)</lang>


=={{header|Slate}}==
==[[Slate]]==
<lang slate>{$a. #(1 2). 'b'. True} at: 2</lang>
<lang slate>{$a. #(1 2). 'b'. True} at: 2</lang>


=={{header|Smalltalk}}==
==[[Smalltalk]]==
<lang smalltalk> #($a $b $c) at: 2</lang>
<lang smalltalk> #($a $b $c) at: 2</lang>


=={{header|Standard ML}}==
==[[Standard ML]]==
val element = Array.sub (array, index)
val element = Array.sub (array, index)


=={{header|Tcl}}==
==[[Tcl]]==
All arrays in Tcl are associative. If "key" is the variable that holds the key of the element to be retrieved, then
All arrays in Tcl are associative. If "key" is the variable that holds the key of the element to be retrieved, then
<lang tcl>set result $array($key)</lang>
<lang tcl>set result $array($key)</lang>
Line 486: Line 490:
<lang tcl>set result [lindex $list $index]</lang>
<lang tcl>set result [lindex $list $index]</lang>


=={{header|Toka}}==
==[[Toka]]==
This retrieves the value 20 from the second item in the array:
This retrieves the value 20 from the second item in the array:


Line 498: Line 502:
table 1 array.get
table 1 array.get


=={{header|X86 assembly}}==
==[[X86 assembly]]==
{{works with|nasm}}
{{works with|nasm}}



Revision as of 23:21, 12 August 2009

Task
Retrieving an Element of an Array
You are encouraged to solve this task according to the task description, using any language you may know.

This task is obsolete. Please do not add new code, and merge existing code to the Arrays task.

In this task, the goal is to retrieve an element of an array.

4D

  ` first element
$elem:=$array{1}

ActionScript

<lang actionscript> var arr:Array = new Array(1,2,3); var myVar:Number = arr[1]; // the value of myVar is: 2 </lang>

Ada

Array indexed by an enumerated type. Ada enumerated types are discrete non-numeric types. <lang ada> type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun); type Daily_Counts is array(Days) of Natural; This_week : Daily_Counts := (200, 212, 175 220, 201, 120, 0); Monday_Sales : Natural; Monday_Sales := This_Week(Mon); </lang> Monday_Sales is assigned 200

ALGOL 68

Translation of: FORTRAN
Works with: ALGOL 68 version Standard - no extensions to language used
Works with: ALGOL 68G version Any - tested with release mk15-0.8b.fc9.i386

ELLA ALGOL 68 translator has restrictions on the number of dimensions and hence cannot compile this code. <lang algol>main:(

PROC get item = (REF [] INT array, INT index)INT:(
  array[index]
);
[4]INT array := (222,444,666,888);
print((get item(array, 3), newline));
OP INIT = (REF[]REAL array)REF[]REAL:( FOR i FROM LWB array TO UPB array DO array[i]:=0.0 OD; array);
OP INIT = (REF[,]REAL array)REF[,]REAL:( FOR i FROM LWB array TO UPB array DO INIT array[i,] OD; array);
[-10:20]REAL a; INIT a;    # a one-dimensional real array indexed from -10 to 20 #
REAL x, y, z;
[5,4]REAL p, q, r; INIT p; # two-dimensional arrays row-indexed from 1 to 5, column-indexed from 1 to 3 #
[2,3,2,2,3,4,2]REAL f;     # a seven-dimensional array (max dimensions allowed is 7) #
  
x := a[-5];                # gets element at index -5 #
y := a[0];                 # gets element at index 0 #
z := a[20];                # gets element at index 20 #
z := p[5,2];               # gets element in row 5, column 2 #

p := q;                    # gets all elements of Q into P #
p[:,2] := a[1:5];          # gets elements at indices 1 to 5 of A into the 2nd column of P #
                           # Note: ALGOL 68 does not have the concept of a slice with a stride #
r[1:4,] := p[2:5,];        # gets 4x4 subarray of P starting in 2nd row into 4x4 subarray of R starting in 1st row #

r[3:5,] := f[1,1,1,1,,,1]  # gets a 3x4 subarray of F into a 3x4 subarray of R starting in row 3 #

)</lang> Output:

       +666

AppleScript

on getArrayValue(array, location)
    -- very important -- The list index starts at 1 not 0
    return item location in array
end getArrayValue

AutoHotkey

Arrays use one-based indexing. Array0 contains the number of elements. <lang autohotkey>string = abcde StringSplit, array, string Loop, % array0

 MsgBox, % array%A_Index%</lang>

AWK

This shows how a string is split into elements of the array a, which is iterated over, and its elements printed to stdout. Note that the order is not as original, because the array is implemented as a hash table.

$ awk 'BEGIN{split("a b c d",a);for(i in a)print a[i]}'
d
a
b
c

C

<lang c> int array_index(int array[], int index) {

   return array[index];
 }</lang>

C#

 int getArrayValue( int values[], int index ) {
   return values[index];
 }

C++

<lang cpp> template<typename T>

 T array_index(T array[], size_t index) {
   return array[index];
 }</lang>

ColdFusion

<cfset arr = ArrayNew(1)>
<cfset arr[1] = "one">
<cfset arr[2] = "2">
<cfset arr[3] = 3>
<cfset var = arr[1]>

The value of var is "one"

ColdFusion Arrays are NOT zero-based, their index begins at 1

Common Lisp

  (defun array-value (array index)
    (aref array index))

D

Generic, template-based method. Allows retriving elements of arrays and objects having opIndex method implemented.

<lang D> // GetElem.d module GetElem;

import tango.core.Variant; import tango.core.Traits;

/// objects must have opIndex method template GetItemType(T) { alias ReturnTypeOf!(T.opIndex) GetItemType; } // specialization for arrays template GetItemType(T : T[]) { alias T GetItemType; }

GetItemType!(T) GetElem(T, U)(T array, U idx) {

   return array[idx];

} </lang>

Sample usage: <lang D> import tango.io.Stdout;

import GetElem;

class SampleContainer {

   static char[][] data = [ "lazy", "fox" "jumped", "over", "dog" ];

public:

   char[] opIndex(uint pos) { return data[pos]; }

}

void main() {

   auto y = new SampleContainer;
   auto x =  [5, 1, 7, 3, 6, 4, 2 ];
   Stdout (GetElem(x, 3)).newline;
   Stdout (GetElem(y, 3)).newline;
   // generate exception
   Stdout (GetElem(x, -1)).newline;

} </lang>

[[Delphi}}/[[Object Pascal}}/Standard Pascal

Arrays in all the flavors of pascal can be of any valid base type, or user defined type (which are all made up of base types) and are multi-dimensional. With Delphi dynamic arrays were defined but had been used in pascal since its inception.

A Static array definition: <lang pascal> foo : array[1..10] of integer; { The base index is ONE }</lang> The base index can be freely chosen: <lang pascal> foo: array[7 .. 16] of integer; { The base index is 7 }</lang> Indeed, the "1 .. 10" resp. "7 .. 16" are actually types: they are integer subrange types. Arrays can also be indexed by enumeration types or enumeration subrange types: <lang pascal> type

 rainbowcolor = (red, orange, yellow, green, blue, violet);
var
 foo: array[rainbowcolor] of integer;
 bar: array[yellow .. blue] of integer;
 i: integer
begin
 i := foo[red]; { allowed indices are red, orange, yellow, green, blue, violet }
 i := bar[green]; { allowed indices are yellow, green, blue }
end;</lang>

A Dynamic Array type in Delphi: <lang delphi> foo : array of integer ; // The base index is ZERO</lang> An "old school" dynamic array in the various flavors of pascal <lang delphi> foo : array[0..0] of integer; // The base index is ZERO</lang> A dynamic array in Extended Pascal: <lang pascal> type

 intarray(n: integer) = array[1 .. n] of integer; { base index 1 }
var
 foo: ^intarray;
begin
 new(foo, 10); { foo now has index 1 to 10 }
 i := foo[2];
 dispose(foo); { get rid of the array }
end;</lang>

In the case of the static array, the compiler generates the code to allocate the required memory to hold 10 integers.

In the Delphi style ---dynamic--- array you must set its length: <lang delphi> SetLength(foo,10); // this array will no hold 10 integers</lang> In the "old school" style of dynamic arrays, you created a point to the zero length declaration and then allocated memory to it with GetMem <lang pascal> pFoo : ^Foo ;

Foo  : array[0..0] of integer ;</lang>

All arrays are accessed the same way regardless of declaration method.

<lang pascal> i : integer ;

i := foo[n] ;</lang>

where n is the array index who's base is either 1 or 0 depending on how it was declared.

E

<lang e>def value := array[index]</lang>

Erlang

Array module work with arrays:
Value = array:get(Index, Array).

Forth

Forth does not have special syntax for array access. Address arithmetic is used to access contiguous memory.

create array 1 , 2 , 3 , 4 ,
array 2 cells + @ .    \ 3

Fortran

In ANSI Fortran 90 or later:

 real, dimension(-10:20) :: a           ! a one-dimensional real array indexed from -10 to 20
 real :: x, y, z
 real, dimension(5,4) :: p, q, r        ! two-dimensional arrays row-indexed from 1 to 5, column-indexed from 1 to 3
 real, dimension(2,3,2,2,3,4,2) :: f    ! a seven-dimensional array (max dimensions allowed is 7)
   
 x = a(-5)             ! gets element at index -5
 y = a(0)              ! gets element at index 0
 z = a(20)             ! gets element at index 20
 z = p(5,2)            ! gets element in row 5, column 2
 
 p = q                 ! gets all elements of Q into P
 p(:,2) = a(1:5)       ! gets elements at indices 1 to 5 of A into the 2nd column of P
 q(1,:) = a(-10:20:10) ! gets elements at indices -10, 0, 10, and 20 into the 1st row of Q (stride of 10)
 q(1,:) = a(::10)      ! gets same elements previous assignment (implicit first and last elements, stride of 10)
 r(1:4,:) = p(2:5,:)   ! gets 4x4 subarray of P starting in 2nd row into 4x4 subarray of R starting in 1st row
 
 a(0:-10:-1) = a(5:15) ! gets elements at indices 5 through 15 and places them in elements at indices 0 through -10
                       ! (in reverse order, stride of -1)
 
 r(3:5,:) = f(1,1,1,1,:,:,1) ! gets a 3x4 subarray of F into a 3x4 subarray of R starting in row 3


Groovy

Define an array

arr = ['groovy', 'is', 'a', 'great', 'language']

First element

arr[0] // *** 'groovy'

Last element, negative indexes

arr[-1] // *** 'language'

Ranges

arr[-3..-1] // *** ['a', 'great', 'language']

Mix n Match

arr[0..2, -1] // *** ['groovy', 'is', 'a', 'language']

Haskell

Arrays can have arbitrary bounds (not restricted to integer values, any instance of Ix will do):

import Data.Array

example = listArray (2,5) ["This", "is", "an", example"]

result = example ! 4

Here, result will be equal to "an". It should be noted that in Haskell lists are often used instead of arrays.

IDL

 ; this is allowed:
 result = arr(5) 
 ; but this is preferred:
 result = arr[5]

The form with square brackets is preferred as it unambiguously constitutes array access, while the version with round ones can conflict with a function call if there are both a function and an array with the same name arr.

J

Arrays are the only way J handles data, so every program that produces a portion of a given array is relevant here. In these few examples emphasis is on the primary verb { ("from").

The natural unit of access in J is the item. (Every piece of data can be treated as a list of [zero or more] items):

   NB. Define example one-axis array, in which each item is an atom
   ]ex1=: 10+ i. 6  NB. ] means display the full array (after =: defines it)
10 11 12 13 14 15
   4 { ex1          NB. Single specific item 
14
   _1 { ex1         NB. Last item, using negative indexing
15
   {: ex1           NB. {: is another way to specify the final item
15
   0 5 2 { ex1      NB. Multiple specific items
10 15 12
 
   NB. Two-axis array, in which each item is a one-axis array
   ]ex2=: 4 6 $ 10+i. 24 
10 11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27
28 29 30 31 32 33
   3 0 { ex2        NB. Item selection is the same as in prior examples
28 29 30 31 32 33
10 11 12 13 14 15
   (<3 4) { ex2     NB. Atom selection (index list length equals array shape length)
32
 
   NB. Four-axis array, shaped 5 by 3 by 2 by 2
   ex4=: 5 3 2 2 $ i. 60
   (<2 1) { ex4     NB. Subarray selection: table at given indexing of top two axes
28 29
30 31

Java

Object element = array[index];

JavaScript

var element = array[index];

print item 1 {10 20 30}   ; 10

LSE64

10 array :array
array 5 [] @     # contents of sixth cell in array

Mathematica

<lang Mathematica>

element = arrayindex

</lang> First index is 1, negative indices count from the back, -1 being the last element. [ [ 0 ] ] returns the head. The [ [ ] ]-command (Part) works not only on a list, it works on everything: graphics, equations, matrices of any dimension et cetera. To dig deeper you can specifiy multiple indices [ [ a, b, c ] ] to get the cth element of the bth element of the ath element. For any level you can specify a range using ;;, i.e.: a;;b;;c would give elements a through b in steps of c. If a is not given it will assume 1, if b is not give it will be -1, if c is not given it will be 1.

MAXScript

item = arr[index]

mIRC Scripting Language

Works with: mIRC Script Editor
Works with: mArray Snippet
 alias readmyarray { echo -a $array_read(MyArray, 2, 3) }

Modula-3

VAR arr := ARRAY [1..3] OF INTEGER {1, 2, 3};
VAR myVar := a[2];

Nial

Nial is an array programming language. Thus it has a variety of ways to retrieve elements of an (even multi-dimensional) array.

Define example one-axis array

myarr := count 6
= 1 2 3 4 5 6

retrieve a specific item

myarr@0
= 1

use pick (pick allows specifying another array as the index for a multidimensional array)

1 pick myarr
= 2

scheme like operations

first myarr
=1
rest myarr
=2 3 4 5 6
front myarr
=1 2 3 4 5

There are quite a few others (too large to list here).

Objective-C

Works with: GNUstep
Works with: Cocoa

<lang objc> NSArray *array;

//...
id element = [array objectAtIndex:index];</lang>

OCaml

let element = array.(index)

Octave

<lang octave>a = [1:10]; disp(a(3));  % display third element of the vector disp(a(3:6));  % display elements from 3 to 6 ("subarray") disp(a(1:2:10));  % display elements at odd indexes (1, 3, 5...) disp(a(2:2:10));  % display elements at even indexes (2, 4, 6...) disp(a(10:-1:1)); % display elements in reversed order</lang>


Perl

Works with: Perl version 5.8.8
$elem = $array[0];

PHP

<lang php> $array = array('php', 'has', 'arrays');

// First element 
$elem  = $array[0];</lang>

Pop11

lvars ar = {1 two 'three'};
lvars elem;
;;; Access second element and assign to variable elem
ar(2) -> elem;

This example uses the simplest possible array (a vector). Pop11 has more general arrays, but in all cases access follows the same pattern, and look the same as procedure (function) call.

Python

Works with: Python version 2.5

The item is an element in a list at a given index

 item = aList[index]

or

To use a list like a stack be it FIFO/LIFO

 aList.pop()  # Pop last item in a list
 aList.pop(0) # Pop first item in a list

Note: When using the pop() method, the element is removed from the list.

R

An element from a vector can be retrieved with the []; slices can be taken using ranges or another vector of indexes.

<lang R>a <- 1:10 # create a vector made of number from 1 to 10 print(a[1]) # print the first element print(a[4:6]) # print elements from 4 to 6 print(a[c(1,8, 9)]) # print element 1, 8 and 9</lang>

Comma separated indexes can be used to index matrix, e.g. <lang R>a <- matrix(c(1,2,3,4), 2, 2) print(a[2,1])</lang>

Ruby

<lang ruby> ary = ['Ruby','rules','big','time']

 #the first element
 element = ary[0]
 #or
 element = ary.first
 # => element = 'Ruby'
 #the last element
 element = ary[-1]
 #or
 element = ary.last
 # => element = 'time'
 #retrieving different values at once
 elements = ary.values_at(0,2,3)
 # => elements = ['Ruby','big','time']
 #select the first element of length 3
 element = ary.find{|el|el.length==3}
 # => element = "big"</lang>

Scheme

Lists are more often used in Scheme than vectors <lang scheme> (vector-ref array index)</lang>

Slate

<lang slate>{$a. #(1 2). 'b'. True} at: 2</lang>

Smalltalk

<lang smalltalk> #($a $b $c) at: 2</lang>

Standard ML

val element = Array.sub (array, index)

Tcl

All arrays in Tcl are associative. If "key" is the variable that holds the key of the element to be retrieved, then <lang tcl>set result $array($key)</lang> If you are looking for a collection of values that are indexed by number, you want a list. List are indexed into with the lindex command: <lang tcl>set result [lindex $list $index]</lang>

Toka

This retrieves the value 20 from the second item in the array:

 3 cells is-array table

 ( Populate the array )
 10 0 table array.put
 20 1 table array.put
 30 2 table array.put
 
 table 1 array.get

X86 assembly

Works with: nasm

This retrieves the third 32bit element of an array made of 4-bytes long (32bit) elements. <lang asm> mov esi, array_offset

mov ebx, 2
mov eax, [esi+ebx*4]</lang>