Retrieving an Element of an Array: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
Line 1: Line 1:
{{task}}
[http://online.rigour.info/cavo-audio/ cavo audio] [http://dir.rigour.info/vcop/ vcop2] [http://dir.rigour.info/google-si/ google si] [http://pagina.rigour.info/microfoni-condensatore/ microfoni condensatore] [http://content.rigour.info/html/canon-/ canon 5 megapixel] [http://gratis.rigour.info/content/view/polti.htm polti 1000] [http://content.rigour.info/html/la-cosa/ la cosa da un altro mondo. special edition] [http://down.rigour.info/blog/el-sol.htm el sol] [http://it.rigour.info/topic/moto-mvagusta/ moto mvagusta] [http://note.rigour.info/content/view/croata-video.htm croata video] [http://milano.rigour.info/ram-hp.htm ram hp] [http://web.rigour.info/il-diabolico/ il diabolico dott. satana] [http://load.rigour.info/view/new/freccia-di.htm freccia di fuoco] [http://note.rigour.info/content/view/fogne-nere.htm fogne nere] [http://italy.rigour.info/blog/modulo-memoria.htm modulo memoria 512 mb] [http://content.rigour.info/html/banca-campania/ banca campania] [http://web.rigour.info/incontro-abbiategrasso/ incontro abbiategrasso] [http://paga.rigour.info/fiat-finance/ fiat finance] [http://milano.rigour.info/videos-porno.htm videos porno full] [http://dir.rigour.info/cocaine/ cocaine] [http://content.rigour.info/html/www-formen/ www formen] [http://here.rigour.info/blog/conan-ragazzo.htm conan ragazzo] [http://here.rigour.info/blog/bo-boss.htm bo boss] [http://note.rigour.info/content/view/diamond-maxtor.htm diamond maxtor] [http://blog.rigour.info/web/novat-pci/ nova-t pci] [http://gratis.rigour.info/content/view/upskirt-gratis.htm upskirt gratis] [http://all.rigour.info/www-sexe/ www sexe com] [http://here.rigour.info/blog/schaerbeek.htm schaerbeek] [http://online.rigour.info/bic-ruga/ bic ruga] [http://note.rigour.info/content/view/mugler-angel.htm mugler angel] [http://all.rigour.info/club-prive/ club prive cava dei tirreni] [http://here.rigour.info/blog/musica-giacomo.htm musica giacomo rondinella] [http://pagina.rigour.info/portatile-dvdrw/ portatile dvdrw] [http://web.rigour.info/giovane-ragazzo/ giovane ragazzo] [http://note.rigour.info/content/view/la-massoneria.htm la massoneria] [http://milano.rigour.info/the-sun.htm the sun and the moon] [http://down.rigour.info/blog/ultima-notte.htm ultima notte a warlock] [http://web.rigour.info/sesso-hard/ sesso hard cazzi grossi in figa] [http://gratis.rigour.info/content/view/alnus.htm alnus] [http://italy.rigour.info/blog/batterie-ricaricabili.htm batterie ricaricabili stilo aa] [http://pagina.rigour.info/gutter-brothers/ gutter brothers] [http://milano.rigour.info/conzoni-romane.htm conzoni romane] [http://italy.rigour.info/blog/tim-ricarica.htm tim ricarica] [http://load.rigour.info/view/new/aspire-living.htm aspire living center] [http://speed.rigour.info/inti.htm inti] [http://paga.rigour.info/lavatrice-rex/ lavatrice rex incasso] [http://web.rigour.info/light-meter/ light meter] [http://pagina.rigour.info/berg-nick/ berg nick] [http://paga.rigour.info/suomi-fi/ suomi24 fi] [http://pagina.rigour.info/for-you/ for you babies] [http://down.rigour.info/blog/simme-napule.htm simme napule] [http://uve.rigour.info/blog/powershot-s.htm powershot s 20 canon] [http://online.rigour.info/internet-security/ internet security ghost] [http://note.rigour.info/content/view/zero-mostel.htm zero mostel] [http://milano.rigour.info/lisola-della.htm lisola della vendetta] [http://web.rigour.info/motoraduni-/ motoraduni 2004 vicenza] [http://dir.rigour.info/dubcova/ dubcova] [http://paga.rigour.info/audi-a/ audi a3 1.9tdi] [http://load.rigour.info/view/new/samuelson-economia.htm samuelson economia] [http://down.rigour.info/blog/ploaia.htm ploaia] [http://paga.rigour.info/nadia-comaneci/ nadia comaneci] [http://uve.rigour.info/blog/sonya-eyes.htm sonya eyes] [http://load.rigour.info/view/new/vacanze-in.htm vacanze in portogallo] [http://content.rigour.info/html/radeon-xxl/ radeon x800xl 256 mb agp] [http://blog.rigour.info/web/estadio-w/ estadio w] [http://uve.rigour.info/blog/video-mitchell.htm video mitchell vieth] [http://up.rigour.info/description/lib/fausto-melotti.htm fausto melotti] [http://online.rigour.info/tavolo-per/ tavolo per proiezione] [http://milano.rigour.info/pir-infrarossi.htm pir infrarossi] [http://pagina.rigour.info/volvo-/ volvo c70 2.4 t] [http://note.rigour.info/content/view/credit-card.htm credit card debt consolidation] [http://it.rigour.info/topic/uccisione-delle/ uccisione delle 2 simone] [http://content.rigour.info/html/julia-chanel/ julia chanel mpegs] [http://it.rigour.info/topic/photo-impact/ photo impact 8] [http://pagina.rigour.info/vaio-sony/ vaio sony s4xp] [http://speed.rigour.info/jordi-carreras.htm jordi carreras tell me why] [http://it.rigour.info/topic/cartina-parigi/ cartina parigi] [http://blog.rigour.info/web/marie-claire/ marie claire d ubaldo] [http://uve.rigour.info/blog/yetisports-part.htm yetisports part 8] [http://note.rigour.info/content/view/motorr.htm motorr] [http://it.rigour.info/topic/toshiba-centrino/ toshiba centrino pm730] [http://guest.rigour.info/library/html/giuseppe-verdi/ giuseppe verdi. due foscari] [http://blog.rigour.info/web/vasco-rossi/ vasco rossi tab] [http://milano.rigour.info/videosdesexogratis.htm videosdesexogratis] [http://uve.rigour.info/blog/jugo-de.htm jugo de col roja] [http://gratis.rigour.info/content/view/paole-parole.htm paole parole] [http://paga.rigour.info/lettore-schede/ lettore schede xd] [http://dir.rigour.info/blan-biliardo/ blan biliardo] [http://note.rigour.info/content/view/inno-paracadutisti.htm inno paracadutisti italiani] [http://web.rigour.info/www-momo/ www momo it] [http://uve.rigour.info/blog/un-uomo.htm un uomo da letto] [http://guest.rigour.info/library/html/momento-doro/ momento doro per ubi soft] [http://pagina.rigour.info/gig-dalessio/ gig dalessio cd cover] [http://content.rigour.info/html/piedi-con/ piedi con calze video] [http://guest.rigour.info/library/html/promessi-sposi/ promessi sposi fumetti] [http://pagina.rigour.info/racconti-calze/ racconti calze] [http://uve.rigour.info/blog/bas-en.htm bas en joel] [http://it.rigour.info/topic/pony/ pony] [http://milano.rigour.info/teen-porn.htm teen porn movies] [http://paga.rigour.info/week-end/ week end relax] [http://speed.rigour.info/lavatrice-gettone.htm lavatrice gettone] [http://dir.rigour.info/guardia/ guardia] [http://content.rigour.info/html/a-caccia/ a caccia di spie] [http://content.rigour.info/html/rav-d/ rav4 d] [http://pagina.rigour.info/km-fiat/ km0 fiat multipla benzina auto km 0] [http://dir.rigour.info/hotel-albergo/ hotel albergo liguria] [http://down.rigour.info/blog/carla-matadinho.htm carla matadinho] [http://blog.rigour.info/web/carolina-desnuda/ carolina desnuda su visto] [http://it.rigour.info/topic/lancia-ypsilon/ lancia ypsilon 1.2] [http://web.rigour.info/hyundai-p/ hyundai 201p] [http://italy.rigour.info/blog/olivia-newtonjohn.htm olivia newton-john] [http://load.rigour.info/view/new/samsung-hi.htm samsung hi fi] [http://all.rigour.info/i-can/ i can fly di robert kunin] [http://up.rigour.info/description/lib/gal-costa.htm gal costa] [http://online.rigour.info/poliziotta-sexi/ poliziotta sexi] [http://load.rigour.info/view/new/scooty-doesn.htm scooty doesn t know] [http://web.rigour.info/fendi-donna/ fendi donna] [http://milano.rigour.info/battle-without.htm battle without honor] [http://load.rigour.info/view/new/fair-game.htm fair game video] [http://dir.rigour.info/mp-trinit/ mp3 trinit] [http://uve.rigour.info/blog/riabilitazione-viscerale.htm riabilitazione viscerale] [http://italy.rigour.info/blog/foto-ragazze.htm foto ragazze in spiaggia] [http://guest.rigour.info/library/html/caronte/ caronte] [http://blog.rigour.info/web/gtr/ gtr] [http://down.rigour.info/blog/nuovi-programmi.htm nuovi programmi] [http://all.rigour.info/kiria-style/ kiria style] [http://uve.rigour.info/blog/las-hurdes.htm las hurdes] [http://speed.rigour.info/spiderman-theme.htm spiderman theme] [http://down.rigour.info/blog/edoardo.htm edoardo 7] [http://guest.rigour.info/library/html/haseni/ haseni] [http://speed.rigour.info/athlon-.htm athlon 64 x2 dual-core] [http://gratis.rigour.info/content/view/foto-valentina.htm foto valentina gioia] [http://online.rigour.info/caimmi/ caimmi] [http://all.rigour.info/concerto-londra/ concerto londra zucchero] [http://gratis.rigour.info/content/view/moto-tgm.htm moto tgm] [http://web.rigour.info/classifica-serie/ classifica serie c calcio] [http://all.rigour.info/white-volta/ white volta] [http://content.rigour.info/html/noleggio-subaru/ noleggio subaru] [http://speed.rigour.info/tv-lcd.htm tv lcd lg 20 pollici] [http://pagina.rigour.info/seat-arosa/ seat arosa 1998] [http://down.rigour.info/blog/mercedes-e.htm mercedes e 200 kompr. avantgarde] [http://milano.rigour.info/sms-anonimi.htm sms anonimi] [http://all.rigour.info/sogni-zie/ sogni zie] [http://content.rigour.info/html/lettore-dvd/ lettore dvd nero] [http://italy.rigour.info/blog/incasso-ci.htm incasso ci] [http://speed.rigour.info/lezioni-di.htm lezioni di tarantella] [http://content.rigour.info/html/biagio-antonacci/ biagio antonacci. live in palermo] [http://dir.rigour.info/sankt-johann/ sankt johann im pongau] [http://online.rigour.info/sms-logo/ sms logo nec e606] [http://speed.rigour.info/cos-e.htm cos e la religione] [http://it.rigour.info/topic/www-caesartour/ www caesartour it] [http://paga.rigour.info/motodacqua/ motodacqua] [http://it.rigour.info/topic/torino-dischi/ torino dischi negozi] [http://blog.rigour.info/web/multipla-jtd/ multipla jtd km 0 dynamic] [http://italy.rigour.info/blog/ef.htm ef 24-85] [http://blog.rigour.info/web/galleggiante-nuoto/ galleggiante nuoto] [http://speed.rigour.info/bmw-piemonte.htm bmw piemonte] [http://all.rigour.info/prestito-berlin/ prestito berlin] [http://pagina.rigour.info/idee-sui/ idee sui regali di natale] [http://milano.rigour.info/ruslana-wild.htm ruslana wild dancer] [http://dir.rigour.info/giuseppe-tubi/ giuseppe tubi arte digitale] [http://it.rigour.info/topic/alfa-romeo/ alfa romeo 166 jtd] [http://dir.rigour.info/file-midi/ file midi alice] [http://note.rigour.info/content/view/oregon-trail.htm oregon trail 3 - anniversary] [http://milano.rigour.info/driver-pk.htm driver p2k] [http://milano.rigour.info/portal-pendidikan.htm portal pendidikan utusan] [http://it.rigour.info/topic/ventola-vcool/ ventola vcool] [http://online.rigour.info/polti/ polti 4500] [http://web.rigour.info/saintmichel/ saint-michel] [http://blog.rigour.info/web/philips-hq/ philips hq 7740] [http://all.rigour.info/nuovo-superman/ nuovo superman] [http://online.rigour.info/progettazione-giardino/ progettazione giardino calabria] [http://uve.rigour.info/blog/passeggiata-damore.htm passeggiata damore] [http://dir.rigour.info/monlight-shadow/ monlight shadow] [http://load.rigour.info/view/new/toscana-natura.htm toscana natura vacanza] [http://paga.rigour.info/bmw-i/ bmw 540i touring] [http://pagina.rigour.info/stampanti-ink/ stampanti ink- jet canon] [http://guest.rigour.info/library/html/the-killing/ the killing of nicholas berg] [http://pagina.rigour.info/printserver-usb/ printserver usb] [http://speed.rigour.info/felpe-sport.htm felpe sport donna abbigliamento sportivo] [http://guest.rigour.info/library/html/stampante-hp/ stampante hp laserjet 1160] [http://it.rigour.info/topic/fiume-sand/ fiume sand creek] [http://blog.rigour.info/web/grecia-last/ grecia last minute voli] [http://blog.rigour.info/web/csi-crime/ csi crime scene investigation il film] [http://milano.rigour.info/maurits-escher.htm maurits escher] [http://guest.rigour.info/library/html/dolby-d/ dolby d] [http://web.rigour.info/milo-gioielli/ milo gioielli] [http://here.rigour.info/blog/siti-gratuiti.htm siti gratuiti per scaricare canzoni] [http://italy.rigour.info/blog/perfect-paz.htm perfect paz] [http://here.rigour.info/blog/alessandro-sposa.htm alessandro sposa rossane] [http://load.rigour.info/view/new/dissipatori-per.htm dissipatori per p4 775] [http://up.rigour.info/description/lib/foto-baby.htm foto baby h] [http://down.rigour.info/blog/foto-marina.htm foto marina la rosa] [http://up.rigour.info/description/lib/unclaimed-money.htm unclaimed money] [http://speed.rigour.info/gangbang-girl.htm gangbang girl] [http://speed.rigour.info/google-vercion.htm google vercion argentina] [http://here.rigour.info/blog/pasticcio-in.htm pasticcio in paradiso] [http://gratis.rigour.info/content/view/club-porshe.htm club porshe] [http://online.rigour.info/belinea/ belinea 10 19 15] [http://online.rigour.info/california-dreaming/ california dreaming royal] {{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]].
Line 15: Line 15:
mov esi, array_offset
mov esi, array_offset
mov ebx, 2
mov ebx, 2
mov eax, [esi ebx*4]
mov eax, [esi+ebx*4]


==[[ActionScript]]==
==[[ActionScript]]==
Line 54: Line 54:
}
}
==[[C plus plus|C ]]==
==[[C plus plus|C++]]==
[[Category:C plus plus|C ]]
[[Category:C plus plus|C++]]


template<typename T>
template<typename T>
Line 139: Line 139:
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


==[[IDL]]==
==[[IDL]]==

Revision as of 15:35, 22 August 2007

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.

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

4D

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

X86 assembly

Assembler: nasm

mov esi, array_offset
mov ebx, 2
mov eax, [esi+ebx*4]

ActionScript

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

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

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

C

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

C#

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

C++

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

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

Delphi/Object Pascal/Turbo Pascal/Standard 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 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

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

Value = lists:nth(Index, List).

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

 ; 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.

Java

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

JavaScript

var element = array[index];

mIRC

Interpeter: mIRC Script Editor

Library: mArray Snippet

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

Perl

Interpreter: Perl 5.8.8

$elem = $array[0];

PHP

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

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

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

 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

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

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

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