Retrieving an Element of an Array: Difference between revisions

tag obsolete (Arrays) and remove {header}s
(tag obsolete (Arrays) and remove {header}s)
Line 1:
{{task|Basic language learning}}In this task, the goal is to retrieve an element of an [[array]].
 
'''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
$elem:=$array{1}
 
=={{header|[[ActionScript}}]]==
<lang actionscript>
var arr:Array = new Array(1,2,3);
Line 12 ⟶ 16:
</lang>
 
=={{header|[[Ada}}]]==
Array indexed by an enumerated type. Ada enumerated types are discrete non-numeric types.
<lang ada>
Line 23 ⟶ 27:
Monday_Sales is assigned 200
 
=={{header|[[ALGOL 68}}]]==
{{trans|FORTRAN}}
 
Line 66 ⟶ 70:
</pre>
 
=={{header|[[AppleScript}}]]==
on getArrayValue(array, location)
-- very important -- The list index starts at 1 not 0
Line 72 ⟶ 76:
end getArrayValue
 
=={{header|[[AutoHotkey}}]]==
Arrays use one-based indexing. Array0 contains the number of elements.
<lang autohotkey>string = abcde
Line 79 ⟶ 83:
MsgBox, % array%A_Index%</lang>
 
=={{header|[[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]}'
Line 87 ⟶ 91:
c
 
=={{header|[[C}}]]==
<lang c> int array_index(int array[], int index) {
return array[index];
}</lang>
 
=={{header|[[C sharp|C#}}]]==
int getArrayValue( int values[], int index ) {
return values[index];
}
 
=={{header|[[C++}}]]==
 
<lang cpp> template<typename T>
Line 104 ⟶ 108:
}</lang>
 
=={{header|[[ColdFusion}}]]==
<cfset arr = ArrayNew(1)>
<cfset arr[1] = "one">
Line 114 ⟶ 118:
''ColdFusion Arrays are '''NOT''' zero-based, their index begins at '''1'''''
 
=={{header|[[Common Lisp}}]]==
(defun array-value (array index)
(aref array index))
 
=={{header|[[D}}]]==
Generic, template-based method. Allows retriving elements
of arrays and objects having opIndex method implemented.
Line 165 ⟶ 169:
</lang>
 
=={{header|[[Delphi}}/{{header|[[Object Pascal}}/Standard {{header|[[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.
Line 213 ⟶ 217:
where n is the array index who's base is either 1 or 0 depending on how it was declared.
 
=={{header|[[E}}]]==
 
<lang e>def value := array[index]</lang>
 
=={{header|[[Erlang}}]]==
:''Array module work with arrays'':
 
Value = array:get(Index, Array).
 
=={{header|[[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
 
=={{header|[[Fortran}}]]==
In ANSI Fortran 90 or later:
real, dimension(-10:20) :: a ! a one-dimensional real array indexed from -10 to 20
Line 251 ⟶ 255:
 
 
=={{header|[[Groovy}}]]==
Define an array
arr = ['groovy', 'is', 'a', 'great', 'language']
Line 267 ⟶ 271:
arr[0..2, -1] // *** ['groovy', 'is', 'a', 'language']
 
=={{header|[[Haskell}}]]==
 
Arrays can have arbitrary bounds (not restricted to integer values, any instance of ''Ix'' will do):
Line 279 ⟶ 283:
Here, ''result'' will be equal to "an". It should be noted that in Haskell lists are often used instead of arrays.
 
=={{header|[[IDL}}]]==
; this is allowed:
result = arr(5)
Line 287 ⟶ 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>.
 
=={{header|[[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").
Line 323 ⟶ 327:
30 31
 
=={{header|[[Java}}]]==
Object element = array[index];
 
=={{header|[[JavaScript}}]]==
var element = array[index];
 
=={{header|[[Logo}}]]==
print item 1 {10 20 30} ; 10
 
=={{header|[[LSE64}}]]==
10 array :array
array 5 [] @ # contents of sixth cell in array
 
=={{header|[[Mathematica}}]]==
<lang Mathematica>
element = array[[index]]
Line 342 ⟶ 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.
 
=={{header|[[MAXScript}}]]==
item = arr[index]
 
=={{header|[[mIRC Scripting Language}}]]==
{{works with|mIRC Script Editor}}
{{works with|mArray Snippet}}
Line 352 ⟶ 356:
alias readmyarray { echo -a $array_read(MyArray, 2, 3) }
 
=={{header|[[Modula-3}}]]==
<pre>
VAR arr := ARRAY [1..3] OF INTEGER {1, 2, 3};
Line 358 ⟶ 362:
</pre>
 
=={{header|[[Nial}}]]==
Nial is an array programming language. Thus it has a variety of ways
to retrieve elements of an (even multi-dimensional) array.
Line 381 ⟶ 385:
There are quite a few others (too large to list here).
 
=={{header|[[Objective-C}}]]==
{{works with|GNUstep}}
 
Line 390 ⟶ 394:
id element = [array objectAtIndex:index];</lang>
 
=={{header|[[OCaml}}]]==
let element = array.(index)
 
=={{header|[[Octave}}]]==
 
<lang octave>a = [1:10];
Line 403 ⟶ 407:
 
 
=={{header|[[Perl}}]]==
{{works with|Perl|5.8.8}}
$elem = $array[0];
 
=={{header|[[PHP}}]]==
<lang php> $array = array('php', 'has', 'arrays');
// First element
$elem = $array[0];</lang>
 
=={{header|[[Pop11}}]]==
lvars ar = {1 two 'three'};
lvars elem;
Line 420 ⟶ 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.
 
=={{header|[[Python}}]]==
{{works with|Python|2.5}}
The item is an element in a list at a given index
Line 432 ⟶ 436:
'''Note:''' When using the pop() method, the element is removed from the list.
 
=={{header|[[R}}]]==
 
An element from a vector can be retrieved with the []; slices can be taken using ranges or another vector of indexes.
Line 445 ⟶ 449:
print(a[2,1])</lang>
 
=={{header|[[Ruby}}]]==
<lang ruby> ary = ['Ruby','rules','big','time']
#the first element
Line 467 ⟶ 471:
# => element = "big"</lang>
 
=={{header|[[Scheme}}]]==
Lists are more often used in Scheme than vectors
<lang scheme> (vector-ref array index)</lang>
 
=={{header|[[Slate}}]]==
<lang slate>{$a. #(1 2). 'b'. True} at: 2</lang>
 
=={{header|[[Smalltalk}}]]==
<lang smalltalk> #($a $b $c) at: 2</lang>
 
=={{header|[[Standard ML}}]]==
val element = Array.sub (array, index)
 
=={{header|[[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>
Line 486 ⟶ 490:
<lang tcl>set result [lindex $list $index]</lang>
 
=={{header|[[Toka}}]]==
This retrieves the value 20 from the second item in the array:
 
Line 498 ⟶ 502:
table 1 array.get
 
=={{header|[[X86 assembly}}]]==
{{works with|nasm}}
 
Anonymous user