Retrieving an Element of an Array: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
(blanked page since people can't / don't read)
 
(87 intermediate revisions by 35 users not shown)
Line 1: Line 1:
{{DeprecatedTask}}
[http://biografiadavidbowie.clung.cn/index.html biografia david bowie] [http://wonted.cn/www-wrid-cup/index.html www wrid cup com] [http://kitmun.cn/yunan.html yunan] [http://knock-knock.cn/www-oksesso/index.html www oksesso] [http://ballifolk.clung.cn/index.html balli folk] [http://buleriamidi.midways.cn/index.html buleria midi] [http://wonted.cn/www-enaip/index.html www enaip it] [http://bratzdadisegnare.romanikki.cn/index.html bratz da disegnare] [http://blanquitasaldesnudo.clung.cn/index.html blanquitas al desnudo] [http://busheblair.midways.cn/index.html bush e blair] [http://wonted.cn/www-tvantioquia/index.html www tvantioquia com] [http://knock-knock.cn/www-csalecce/index.html www csalecce] [http://knock-knock.cn/www-bibdarte/index.html www bibdarte it] [http://basumba.kittiss.cn/index.html basumba] [http://kitmun.cn/yesterday-when.html yesterday when i was young] [http://brecovich.kittiss.cn/index.html brecovich] [http://bachair.romanikki.cn/index.html bach air] [http://britneyspearstoxic.kittiss.cn/index.html britney spears toxic] [http://wonted.cn/wamma-wamma/index.html wamma wamma wamma] [http://ballobrasile.romanikki.cn/index.html ballo brasile] [http://wonted.cn/www-ksuave/index.html www ksuave com] [http://knock-knock.cn/www-culladelbimbo/index.html www culladelbimbo it] [http://barracuda.romanikki.cn/index.html barracuda] [http://snailtail.cn/www-intimissimi/index.html www intimissimi] [http://quoits.cn/zelig-mp3.html zelig mp3] [http://blueifyoucomeback.kittiss.cn/index.html blue if you come back] [http://wonted.cn/www-asl8cagliari/index.html www asl8cagliari it] [http://brooklinbounce.clung.cn/index.html brooklin bounce] [http://snailtail.cn/www-policia/index.html www policia gov co 51] [http://bennychao.romanikki.cn/index.html benny chao] [http://bluecdonelove.midways.cn/index.html blue cd one love] [http://knock-knock.cn/www-cronache/index.html www cronache rapimenti it] [http://quoits.cn/alterazioni.html alterazioni della realtà] [http://wonted.cn/www-commercioelettronicoitaliano/index.html www commercioelettronicoitaliano it] [http://kitmun.cn/zuchero.html zuchero everybody] [http://besenzoni.romanikki.cn/index.html besenzoni] [http://quoits.cn/zaion-y-lenox.html zaion y lenox] [http://blaackeyedpeas.midways.cn/index.html blaack eyed peas] [http://kitmun.cn/yvonne-caterfeld.html yvonne caterfeld] [http://kitmun.cn/you-re-my.html you re my heart yo re my soul] [http://wonted.cn/www-svago/index.html www svago com] [http://wonted.cn/www-rete4-com/index.html www rete4 com] [http://kitmun.cn/yaesu-vx7.html yaesu vx7] [http://bennibennassi.kittiss.cn/index.html benni bennassi] [http://quoits.cn/abissi.html abissi] [http://wonted.cn/www-santi-beati/index.html www santi beati it] [http://snailtail.cn/xutos-pontapes/index.html xutos pontapes] [http://bubblintraduzione.kittiss.cn/index.html bubblin traduzione] [http://kitmun.cn/zero-assoluto.html zero assoluto video] [http://knock-knock.cn/www-cesat/index.html www cesat com] [http://b12feat.romanikki.cn/index.html b 12 feat] [http://knock-knock.cn/where-you-lead/index.html where you lead carol king] [http://blondiecallme.midways.cn/index.html blondie call me] [http://bluestaff.romanikki.cn/index.html blue staff] [http://knock-knock.cn/www-stresa-villaggi/index.html www stresa villaggi] [http://biografiasdequimicos.kittiss.cn/index.html biografias de quimicos] [http://blogssex.romanikki.cn/index.html blogs sex] [http://knock-knock.cn/www-betamotor/index.html www betamotor it] [http://knock-knock.cn/www-eritornodate/index.html www eritornodate it] [http://blackfat.romanikki.cn/index.html black fat] [http://snailtail.cn/www-provveditorato/index.html www provveditorato com] [http://quoits.cn/a-parigi-in-vacanza.html a parigi in vacanza] [http://knock-knock.cn/when-i-die/index.html when i die] [http://kitmun.cn/ytunnelpro272.html ytunnelpro272] [http://bassielettricicondor.kittiss.cn/index.html bassi elettrici condor] [http://buonaseradottoreclaudiamori.clung.cn/index.html buonasera dottore claudia mori] [http://budspencerterencehillmp3.midways.cn/index.html budspencer terence hill mp3] [http://basketusa.clung.cn/index.html basket u s a] [http://snailtail.cn/xircom/index.html xircom] [http://snailtail.cn/xbox-media/index.html xbox media play] [http://blameitonthe.clung.cn/index.html blame it on the] [http://wonted.cn/www-acquisto/index.html www acquisto casa it] [http://baviera.kittiss.cn/index.html baviera] [http://snailtail.cn/www-gruppofabbri/index.html www gruppofabbri it] [http://benassibrostipmyheart.kittiss.cn/index.html benassi bros tip my heart] [http://wonted.cn/www-tesi/index.html www tesi online it] [http://beallsstore.clung.cn/index.html bealls store] [http://britneyspearporno.midways.cn/index.html britney spear porno] [http://snailtail.cn/yo-no-piedo/index.html yo no piedo la luna] [http://blueeifel67.clung.cn/index.html blue eifel 67] [http://ballaballadibalsamo.kittiss.cn/index.html balla balla di balsamo] [http://kitmun.cn/yanni.html yanni] [http://bamboleo.romanikki.cn/index.html bamboleo] [http://wonted.cn/www-rotter/index.html www rotter] [http://baglionivia.kittiss.cn/index.html baglioni via] [http://bigleydecapitatovideo.clung.cn/index.html bigley decapitato video] [http://kitmun.cn/yahu-com-mx.html yahu com mx] [http://wonted.cn/www-lisalipps/index.html www lisalipps com] [http://bolujemusebi.midways.cn/index.html bolujem u sebi] [http://bigleykennet.midways.cn/index.html bigley kennet] [http://bonnyandclyde.kittiss.cn/index.html bonny and clyde] [http://bloodhoundgangihopeyoudie.clung.cn/index.html bloodhound gang i hope you die] [http://battisterodiparma.clung.cn/index.html battistero di parma] [http://kitmun.cn/yety-sport5.html yety sport5] [http://bigliettodiauguri.clung.cn/index.html biglietto di auguri] [http://besamemuchodievora.clung.cn/index.html besame mucho di evora] [http://kitmun.cn/youkali.html youkali] [http://knock-knock.cn/www-patrizio/index.html www patrizio rispoli it] [http://quoits.cn/a-077,-sfida.html a 077, sfida ai killers] [http://kitmun.cn/zion-ahora.html zion ahora] [http://quoits.cn/zeljko-joksimovic.html zeljko joksimovic video s] [http://barcheoccasione.clung.cn/index.html barche occasione] [http://knock-knock.cn/www-wwe-con/index.html www wwe con] [http://benasibross.fast-road.cn/index.html benasi bross] [http://kitmun.cn/yoyo-yaya-balli.html yoyo yaya balli di gruppo] [http://banchieri.romanikki.cn/index.html banchieri] [http://snailtail.cn/xsat-fun-hexa/index.html xsat fun hexa] [http://bindiya.midways.cn/index.html bindiya] [http://knock-knock.cn/wanna-rock/index.html wanna rock] [http://beppebarra.kittiss.cn/index.html beppe barra] [http://boogiepaoloconte.clung.cn/index.html boogie paolo conte] [http://knock-knock.cn/www-libri-ro/index.html www libri ro] [http://wonted.cn/www-appendice/index.html www appendice it] [http://knock-knock.cn/www-harry-potter/index.html www harry potter it] [http://knock-knock.cn/www-giannanannini/index.html www giannanannini com] [http://botellaenvenenada.midways.cn/index.html botella envenenada] [http://bbaiocodes.fast-road.cn/index.html bbaio codes] [http://snailtail.cn/www-portaleaziende/index.html www portaleaziende it] [http://boxercane.fast-road.cn/index.html boxer cane] [http://bedbreakfasttrentino.kittiss.cn/index.html bed breakfast trentino] [http://snailtail.cn/www-sms-mania/index.html www sms mania it] [http://behindblueeyeslimpbiskit.kittiss.cn/index.html behind blue eyes limp biskit] [http://bmwminituning.midways.cn/index.html bmw mini tuning] [http://bellalavita.kittiss.cn/index.html bella la vita] [http://knock-knock.cn/www-yiyeavila/index.html www yiyeavila org] [http://brittanymurphy.midways.cn/index.html brittany murphy] [http://biodivesita.kittiss.cn/index.html biodivesita] [http://banbinenude.romanikki.cn/index.html banbine nude] [http://knock-knock.cn/wwwregione/index.html wwwregione lombardia it] [http://snailtail.cn/xmen2/index.html xmen2] [http://beniniantonio.midways.cn/index.html benini antonio] [http://beanieman.clung.cn/index.html beanie man] [http://quoits.cn/arcana.html arcana] [http://quoits.cn/zucchero-mana.html zucchero mana] [http://snailtail.cn/xem-anh-khoa/index.html xem anh khoa than hong nhung] [http://benkingsley.midways.cn/index.html ben kingsley] [http://barzellettaarchivio.kittiss.cn/index.html barzelletta archivio] [http://kitmun.cn/yankse-progdvb.html yankse progdvb] [http://betegsegek.fast-road.cn/index.html betegsegek] [http://snailtail.cn/www-ssk-com/index.html www ssk com] [http://knock-knock.cn/www-chirca/index.html www chirca it] [http://wonted.cn/www-inps-firenze/index.html www inps firenze it] [http://babilonsexroncadelle.midways.cn/index.html babilonsex roncadelle] [http://wonted.cn/www-jetix-it/index.html www jetix it] [http://bergh.clung.cn/index.html bergh] [http://berlina.romanikki.cn/index.html berlina] [http://bolaffi.kittiss.cn/index.html bolaffi] [http://kitmun.cn/ziony-lennox.html ziony lennox] [http://quoits.cn/ammutinamento.html ammutinamento nello spazio] [http://kitmun.cn/yo-sin-ti.html yo sin ti] {{task}}

'''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]].

==[[4D]]==
[[Category:4D]]

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

==[[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))

==[[Delphi/Object Pascal/Turbo Pascal/Standard Pascal]]==
[[Category:Pascal]]

Array's 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 inseption.

A Static array defintion:
foo : array[1..10] of integer; { The base index is ONE }
The base index can be freely chosen:
foo: array[7 .. 16] of integer; { The base index is 7 }
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:
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
2000
are yellow, green, blue }
end;
A Dynamic Array type in Delphi:
foo : array of integer ; // The base index is ZERO
An "old school" dynamic array in the various flavors of pascal
foo : array[0..0] of integer; // The base index is ZERO
A dynamic array in Extended 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;
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:
SetLength(foo,10); // this array will no hold 10 integers
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
pFoo : ^Foo ;
Foo : array[0..0] of integer ;

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

i : integer ;
i := foo[n] ;
where n is the array index who's base is either 1 or 0 depending on how it was declared.

==[[Erlang]]==
[[Category:Erlang]]

Erlang lists are 1-based which means that the index must be within the bounds (1 .. length(List)):

Value = lists:nth(Index, List).

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

==[[IDL]]==
[[Category: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 <tt>arr</tt>.

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

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

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

var element = array[index];

==[[mIRC]]==
[[Category:mIRC Scripting Language]]

'''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];

==[[Pop11]]==
[[Category: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]]==
[[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)

==[[Toka]]==
[[Category:Toka]]

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

3 cells is-array table

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

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.