Retrieving an Element of an Array: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
(revert spam)
Line 1: Line 1:
{{task}}
[http://italnet.net.cn/img/regole-pokemon/ regole pokemon] [http://zodip.info/description/test-d.htm test d ingresso scenze dell educazione] [http://zodip.info/description/master-league.htm master league] [http://zodip.info/description/pp-cellulare.htm p2p cellulare] [http://puli.org.cn/web/epson-t/ epson t028] [http://nibko.info/data/dragonball-z.htm dragonball z clips video] [http://puli.org.cn/web/www-amanda/ www amanda please com] [http://puli.org.cn/web/foto-di/ foto di torture in iraq] [http://italnet.net.cn/img/shinco-sdp/ shinco sdp 1850] [http://hangzhau.cn/html/colonna-sonora/ colonna sonora u2] [http://zodip.info/description/ray-ban.htm ray ban 4057] [http://zodip.info/description/prequel.htm prequel] [http://zodip.info/description/lascari.htm lascari] [http://hangzhau.cn/html/freecom-media/ freecom media player 3] [http://nibko.info/data/nonnette.htm nonnette] [http://nibko.info/data/satellite.htm satellite] [http://hangzhau.cn/html/singoli-vogliosi/ singoli vogliosi] [http://nibko.info/data/severina-vuckovic.htm severina vuckovic] [http://italnet.net.cn/img/acer-/ acer - notebook travelmate 4101lmi] [http://nibko.info/data/siamesi.htm siamesi] [http://hangzhau.cn/html/nokia-/ nokia 6680 blue] [http://puli.org.cn/web/programmitv/ programmitv] [http://italnet.net.cn/img/furka-passo/ furka, passo della-] [http://nibko.info/data/www-bikiny.htm www bikiny com] [http://hangzhau.cn/html/fortuna-gps/ fortuna gps] [http://zodip.info/description/cartina-roma.htm cartina roma] [http://italnet.net.cn/img/l-eternita/ l eternita giorgia] [http://italnet.net.cn/img/sindrome-stendhal/ sindrome stendhal] [http://zodip.info/description/sexx.htm sexx] [http://hangzhau.cn/html/tuoitrequay/ tuoitrequay] [http://italnet.net.cn/img/d-mustek/ d30 mustek] [http://hangzhau.cn/html/media-center/ media center] [http://puli.org.cn/web/sto-cazzo/ sto cazzo] [http://zodip.info/description/panasonic-lumix.htm panasonic lumix lc1] [http://nibko.info/data/io-voglio.htm io voglio vivere i nomadi] [http://hangzhau.cn/html/www-damiani/ www damiani it] [http://nibko.info/data/downoland.htm downoland] [http://nibko.info/data/marco-massafra.htm marco massafra] [http://zodip.info/description/video-impossible.htm video impossible is nothing] [http://zodip.info/description/new-beauty.htm new beauty magazine] [http://zodip.info/description/racconti-sadomaso.htm racconti sadomaso bdsm] [http://italnet.net.cn/img/fujiko/ fujiko] [http://italnet.net.cn/img/vida-loca/ vida loca] [http://puli.org.cn/web/juliana-rubinstein/ juliana rubinstein] [http://italnet.net.cn/img/te-i/ te i pe] [http://italnet.net.cn/img/denon-dvd/ denon dvd 1920] [http://hangzhau.cn/html/prima-guerra/ prima guerra] [http://hangzhau.cn/html/mp-tuxedomoon/ mp3 tuxedomoon] [http://hangzhau.cn/html/sassari-negoio/ sassari negoio] [http://puli.org.cn/web/castelleone/ castelleone] [http://puli.org.cn/web/costo-sera/ costo sera capodanno] [http://nibko.info/data/mirc-romania.htm mirc romania] [http://nibko.info/data/crack-nero.htm crack nero7] [http://nibko.info/data/samsung-stampanti.htm samsung stampanti laser] [http://italnet.net.cn/img/hook-up/ hook up] [http://hangzhau.cn/html/pro/ pro1] [http://italnet.net.cn/img/grande-fratello/ grande fratello 3] [http://italnet.net.cn/img/smsgratis/ smsgratis] [http://nibko.info/data/fighe-animate.htm fighe animate] [http://puli.org.cn/web/myskina-topless/ myskina topless] [http://italnet.net.cn/img/orange-number/ orange number] [http://hangzhau.cn/html/golino-fiorello/ golino fiorello] [http://italnet.net.cn/img/jungle-adventures/ jungle adventures 3] [http://zodip.info/description/dianna.htm dianna] [http://italnet.net.cn/img/obiettivi-sony/ obiettivi sony] [http://nibko.info/data/cartone-animato.htm cartone animato animale] [http://nibko.info/data/yamaha-ax.htm yamaha ax 396] [http://nibko.info/data/boris.htm boris] [http://nibko.info/data/la-fanciulla.htm la fanciulla col violoncello] [http://nibko.info/data/carlie-de.htm carlie de duck] [http://nibko.info/data/yoto.htm yoto] [http://nibko.info/data/i-q.htm i q] [http://zodip.info/description/net-enterprise.htm net enterprise] [http://hangzhau.cn/html/cuffia-dj/ cuffia dj sony] [http://puli.org.cn/web/worms-/ worms 4 mayhem xbox] [http://nibko.info/data/bmw-.htm bmw 725 tds 1999] [http://nibko.info/data/miracle-lancome.htm miracle lancome] [http://italnet.net.cn/img/zombie-major/ zombie major tom] [http://nibko.info/data/law-and.htm law and order] [http://nibko.info/data/grazie-roma.htm grazie roma antonello venditti] [http://italnet.net.cn/img/crea-e/ crea e colora facce e corpi] [http://italnet.net.cn/img/giselle/ giselle] [http://italnet.net.cn/img/realizzazione-campi/ realizzazione campi di calcio] [http://zodip.info/description/ip-telephony.htm ip telephony] [http://hangzhau.cn/html/etero-per/ etero per lui] [http://zodip.info/description/sex-me.htm sex me now] [http://hangzhau.cn/html/philips-genie/ philips genie batteria batterie per] [http://italnet.net.cn/img/il-giro/ il giro del mondo in ottanta minuti] [http://puli.org.cn/web/biggest-boobs/ biggest boobs] [http://nibko.info/data/briganti-si.htm briganti si mora] [http://zodip.info/description/scheda-audio.htm scheda audio per notebook] [http://hangzhau.cn/html/forno-ventilato/ forno ventilato smeg] [http://hangzhau.cn/html/piani-di/ piani di cottura a legna] [http://hangzhau.cn/html/il-crepuscolo/ il crepuscolo degli eroi] [http://hangzhau.cn/html/batteria-per/ batteria per motorola c350] [http://italnet.net.cn/img/waitec/ waitec 7] [http://italnet.net.cn/img/moondance-buble/ moondance buble] [http://italnet.net.cn/img/doremifasol/ doremifasol] [http://zodip.info/description/corse-auto.htm corse auto ralli] [http://zodip.info/description/natalino-otto.htm natalino otto] [http://zodip.info/description/party-started.htm party started] [http://nibko.info/data/diablo-cd.htm diablo cd key] [http://italnet.net.cn/img/xvid-divx/ xvid divx wma lettori dvd e] [http://hangzhau.cn/html/rossignol-sci/ rossignol sci comp 9 j] [http://nibko.info/data/chrisler-it.htm chrisler it] [http://italnet.net.cn/img/el-reloj/ el reloj] [http://italnet.net.cn/img/www-agriturismi/ www agriturismi marchigiani it] [http://hangzhau.cn/html/audigy-zs/ audigy2 zs] [http://zodip.info/description/don-t.htm don t make me pay] [http://italnet.net.cn/img/carte-prepagate/ carte prepagate] [http://zodip.info/description/pavillion-hp.htm pavillion hp] [http://hangzhau.cn/html/videoproiettore-dlp/ videoproiettore dlp 2000 ansi lumen] [http://zodip.info/description/study.htm study] [http://puli.org.cn/web/radeon-pci/ radeon pci express] [http://nibko.info/data/flash-mario.htm flash mario bros] [http://puli.org.cn/web/sci-accessori/ sci accessori] [http://nibko.info/data/celebrita-erotiche.htm celebrita erotiche] [http://puli.org.cn/web/lungo-abiti/ lungo abiti donna abbigliamento] [http://zodip.info/description/nuova-porsche.htm nuova porsche 911 auto nuove] [http://italnet.net.cn/img/regione-c/ regione c] [http://zodip.info/description/elena-studentessa.htm elena studentessa in chimica] [http://nibko.info/data/memoria-ram.htm memoria ram 333 acer aspire] [http://nibko.info/data/banca-carige.htm banca carige online] [http://italnet.net.cn/img/uni/ uni 10349] [http://nibko.info/data/kardon.htm kardon 130] [http://nibko.info/data/free-for.htm free for love] [http://puli.org.cn/web/guasti-clio/ guasti clio] [http://italnet.net.cn/img/agenzia-spaziale/ agenzia spaziale italiana] [http://hangzhau.cn/html/norton-internet/ norton internet security 2005 ghost 9 0] [http://zodip.info/description/pentax-dl.htm pentax dl] [http://puli.org.cn/web/montand/ montand] [http://nibko.info/data/dvd-player.htm dvd player jvc] [http://hangzhau.cn/html/cartina-geografia/ cartina geografia greca] [http://italnet.net.cn/img/golf-/ golf 1.9 tdi 130 cv time] [http://puli.org.cn/web/lettori-mp/ lettori mp3 con batteria a litio] [http://zodip.info/description/antenna-unicars.htm antenna unicars] [http://italnet.net.cn/img/hp-x/ hp 4345x] [http://hangzhau.cn/html/jeans-uomo/ jeans uomo armani] [http://italnet.net.cn/img/hentai-lesbian/ hentai lesbian] [http://zodip.info/description/il-grande.htm il grande sperone] [http://puli.org.cn/web/foto-dei/ foto dei personaggi del telefilm paso ad] [http://puli.org.cn/web/marsa-/ marsa sicurezza snc] [http://hangzhau.cn/html/flybywire/ fly-by-wire] [http://puli.org.cn/web/fakaofo/ fakaofo] [http://zodip.info/description/haifa.htm haifa] [http://hangzhau.cn/html/graz-cose/ graz cose fare] [http://italnet.net.cn/img/creative-megaworks/ creative megaworks 2 1 thx] [http://italnet.net.cn/img/vostro-figlio/ vostro figlio e fratello] [http://hangzhau.cn/html/leonhardt/ leonhardt] [http://hangzhau.cn/html/coolpix-/ coolpix 5700 nikon] [http://nibko.info/data/erci.htm erci] [http://puli.org.cn/web/auto-friuli/ auto friuli] [http://hangzhau.cn/html/cgp/ cgp] [http://italnet.net.cn/img/eastern-province/ eastern province (zambia)] [http://zodip.info/description/lettore-usb.htm lettore usb pcmcia] [http://hangzhau.cn/html/inizio-scuole/ inizio scuole] [http://zodip.info/description/tenchu-stealth.htm tenchu stealth assasins] [http://nibko.info/data/martha-lasciami.htm martha lasciami viva] [http://hangzhau.cn/html/ddr-sodimm/ ddr2 sodimm 1gb] [http://italnet.net.cn/img/mercedes-serie/ mercedes serie sl] [http://zodip.info/description/ragazzo-forli.htm ragazzo forli] [http://nibko.info/data/porcile.htm porcile] [http://zodip.info/description/vetro-pias.htm vetro pias] [http://hangzhau.cn/html/professional-regulations/ professional regulations commission] [http://italnet.net.cn/img/ramazzotti-eros/ ramazzotti eros stilelibero] [http://hangzhau.cn/html/tacco-alto/ tacco alto roberto cavalli calzature] [http://puli.org.cn/web/foto-di/ foto di cazzi piccoli] [http://italnet.net.cn/img/materasso-lattice/ materasso lattice materassi] [http://puli.org.cn/web/clandestino/ clandestino] [http://zodip.info/description/divx-nortek.htm divx nortek lettori dvd e] [http://hangzhau.cn/html/lultimo-colpo/ lultimo colpo in canna] [http://italnet.net.cn/img/santa-fe/ santa fe km zero] [http://nibko.info/data/dieta-sportiva.htm dieta sportiva] [http://nibko.info/data/prada-uomo.htm prada uomo cinture] [http://zodip.info/description/il-maschio.htm il maschio ruspante] [http://hangzhau.cn/html/processori-intel/ processori intel 660 775] [http://zodip.info/description/vigilidelfuoco-it.htm vigilidelfuoco it] [http://hangzhau.cn/html/www-pokemon/ www pokemon porno com] [http://hangzhau.cn/html/www-peugeut/ www peugeut] [http://hangzhau.cn/html/sede-fastweb/ sede fastweb] [http://italnet.net.cn/img/il-mattone/ il mattone] [http://hangzhau.cn/html/just-cavalli/ just cavalli profumo] [http://hangzhau.cn/html/video-guardoni/ video guardoni gratis] [http://nibko.info/data/sony-cybershot.htm sony cybershot dsch1] [http://puli.org.cn/web/wd-h/ wd h34 canon] [http://italnet.net.cn/img/roxx/ roxx] [http://italnet.net.cn/img/offerte-lavoro/ offerte lavoro stlisti a lecce] [http://zodip.info/description/b-w.htm b w asw650] [http://puli.org.cn/web/uomo-immagine/ uomo immagine] [http://hangzhau.cn/html/ministero-trasporti/ ministero trasporti] [http://puli.org.cn/web/hub-con/ hub con alimentatore] [http://italnet.net.cn/img/fred-perry/ fred perry uomo] [http://hangzhau.cn/html/mammografo/ mammografo] [http://puli.org.cn/web/la-chiave/ la chiave misteriosa] [http://nibko.info/data/lowepro-photo.htm lowepro photo trekker aw ii] [http://nibko.info/data/tps-key.htm tps key lang] [http://nibko.info/data/dargostea.htm dargostea] [http://hangzhau.cn/html/lacie-tb/ lacie 1tb hard disk e floppy] [http://zodip.info/description/victoria-silvstedt.htm victoria silvstedt] [http://zodip.info/description/comune-parma.htm comune parma] {{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 12:11, 2 September 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