Retrieving an Element of an Array: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
(blanked page since people can't / don't read)
 
(137 intermediate revisions by 43 users not shown)
Line 1: Line 1:
{{task}}
{{DeprecatedTask}}

'''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]].
In this task, the goal is to retrieve an element of an [[array]].

==[[X86 assembly]]==
[[Category:x86 assembly]]

'''Assembler:''' nasm
mov esi, array_offset
mov ebx, 2
mov eax, [esi+ebx*4]

==[[ActionScript]]==
[[Category:ActionScript]]
var arr:Array = new Array(1,2,3);
var myVar:Number = arr[1];
// the value of myVar is: 2

==[[Ada]]==
[[Category:Ada]]
Array indexed by an enumerated type. Ada enumerated types are discrete non-numeric types.
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);
Monday_Sales is assigned 200

==[[AppleScript]]==
[[Category:AppleScript]]
on getArrayValue(array, location)
-- very important -- The list index starts at 1 not 0
return item location in array
end getArrayValue

==[[C]]==
[[Category:C]]

int array_index(int array[], int index) {
return array[index];
}

==[[C sharp|C#]]==
[[Category:C sharp|C#]]

int getArrayValue( int values[], int index ) {
return values[index];
}
==[[C plus plus|C++]]==
[[Category:C plus plus|C++]]

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

==[[ColdFusion]]==
[[Category: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]]==
[[Category:Common Lisp]]

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

==[[Java]]==
[[Category:Java]]

public Object getArrayElem(Object[] array, int pos) {
return array[pos];
}

==[[JavaScript]]==
[[Category:JavaScript]]

var element = array[index];

==[[mIRC]]==
[[Category:mIRC]]

'''Interpeter:''' mIRC Script Editor

'''Library:''' [[mArray Snippet]]

[[Category:mArray Snippet]]

alias readmyarray { echo -a $array_read(MyArray, 2, 3) }

==[[Perl]]==
[[Category:Perl]]

'''Interpreter:''' [[Perl]] 5.8.8
$elem = $array[0];

==[[PHP]]==
[[Category:PHP]]

$array = array('php', 'has', 'arrays');
// First element
$elem = $array[0];

==[[Python]]==
[[Category:Python]]

'''Interpreter:''' Python 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.

==[[Ruby]]==
[[Category: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"

==[[Smalltalk]]==
[[Category:Smalltalk]]

"Retrieve second element of an array"
index := 2
element := anArray at: index

==[[Tcl]]==
[[Category:Tcl]]

All arrays in Tcl are associative. If "key" is the variable that holds the key of the element to be retrieved, then

set result $array($key)

Latest revision as of 15:06, 29 November 2019

Retrieving an Element of an Array was a programming task. It has been deprecated for reasons that are discussed in its talk page.

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.