Retrieving an Element of an Array: Difference between revisions

From Rosetta Code
Content added Content deleted
(revert spam)
mNo edit summary
Line 1: Line 1:
[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}}
{{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 97: Line 97:
begin
begin
i := foo[red]; { allowed indices are red, orange, yellow, green, blue, violet }
i := foo[red]; { allowed indices are red, orange, yellow, green, blue, violet }
i := bar[green]; { allowed indices are yellow, green, blue }
i := bar[green]; { allowed indices
2000
are yellow, green, blue }
end;
end;
A Dynamic Array type in Delphi:
A Dynamic Array type in Delphi:
Line 139: Line 141:
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 13:43, 10 September 2007

biografia david bowie www wrid cup com yunan www oksesso balli folk buleria midi www enaip it bratz da disegnare blanquitas al desnudo bush e blair www tvantioquia com www csalecce www bibdarte it basumba yesterday when i was young brecovich bach air britney spears toxic wamma wamma wamma ballo brasile www ksuave com www culladelbimbo it barracuda www intimissimi zelig mp3 blue if you come back www asl8cagliari it brooklin bounce www policia gov co 51 benny chao blue cd one love www cronache rapimenti it alterazioni della realtà www commercioelettronicoitaliano it zuchero everybody besenzoni zaion y lenox blaack eyed peas yvonne caterfeld you re my heart yo re my soul www svago com www rete4 com yaesu vx7 benni bennassi abissi www santi beati it xutos pontapes bubblin traduzione zero assoluto video www cesat com b 12 feat where you lead carol king blondie call me blue staff www stresa villaggi biografias de quimicos blogs sex www betamotor it www eritornodate it black fat www provveditorato com a parigi in vacanza when i die ytunnelpro272 bassi elettrici condor buonasera dottore claudia mori budspencer terence hill mp3 basket u s a xircom xbox media play blame it on the www acquisto casa it baviera www gruppofabbri it benassi bros tip my heart www tesi online it bealls store britney spear porno yo no piedo la luna blue eifel 67 balla balla di balsamo yanni bamboleo www rotter baglioni via bigley decapitato video yahu com mx www lisalipps com bolujem u sebi bigley kennet bonny and clyde bloodhound gang i hope you die battistero di parma yety sport5 biglietto di auguri besame mucho di evora youkali www patrizio rispoli it a 077, sfida ai killers zion ahora zeljko joksimovic video s barche occasione www wwe con benasi bross yoyo yaya balli di gruppo banchieri xsat fun hexa bindiya wanna rock beppe barra boogie paolo conte www libri ro www appendice it www harry potter it www giannanannini com botella envenenada bbaio codes www portaleaziende it boxer cane bed breakfast trentino www sms mania it behind blue eyes limp biskit bmw mini tuning bella la vita www yiyeavila org brittany murphy biodivesita banbine nude wwwregione lombardia it xmen2 benini antonio beanie man arcana zucchero mana xem anh khoa than hong nhung ben kingsley barzelletta archivio yankse progdvb betegsegek www ssk com www chirca it www inps firenze it babilonsex roncadelle www jetix it bergh berlina bolaffi ziony lennox ammutinamento nello spazio yo sin ti

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

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

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