Retrieving an Element of an Array: Difference between revisions
Content added Content deleted
No edit summary |
(Properly ordering the languages) |
||
Line 3: | Line 3: | ||
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== |
||
''' |
'''Assembler:''' nasm |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
==[[C]]== |
==[[C]]== |
||
int array_index(int array[], int index) { |
int array_index(int array[], int index) { |
||
return array[index]; |
return array[index]; |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
Line 17: | Line 25: | ||
T array_index(T *array, int index) { |
T array_index(T *array, int index) { |
||
return array[index]; |
return array[index]; |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
==[[Common Lisp]]== |
==[[Common Lisp]]== |
||
(defun array-value (array index) |
(defun array-value (array index) |
||
(aref array index)) |
(aref array index)) |
||
⚫ | |||
'''Interpeter:''' mIRC Script Editor |
|||
⚫ | |||
⚫ | |||
==[[Perl]]== |
==[[Perl]]== |
||
Line 34: | Line 41: | ||
$elem = $array[0]; |
$elem = $array[0]; |
||
==[[PHP]]== |
==[[PHP]]== |
||
Line 40: | Line 46: | ||
// First element |
// First element |
||
$elem = $array[0]; |
$elem = $array[0]; |
||
==[[Python]]== |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
==[[Ruby]]== |
==[[Ruby]]== |
||
Line 62: | Line 81: | ||
element = ary.find{|el|el.length==3} |
element = ary.find{|el|el.length==3} |
||
# => element = "big" |
# => element = "big" |
||
==x86 assembly== |
|||
'''Assembler:''' nasm |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ |
Revision as of 22:01, 23 January 2007
Retrieving an Element of an Array
You are encouraged to solve this task according to the task description, using any language you may know.
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.
x86 assembly
Assembler: nasm
mov esi, array_offset mov ebx, 2 mov eax, [esi+ebx*4]
C
int array_index(int array[], int index) { return array[index]; }
C#
int getArrayValue( int values[], int index ) { return values[index]; }
C++
template <class T> T array_index(T *array, int index) { return array[index]; }
Common Lisp
(defun array-value (array index) (aref 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];
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"