Assigning Values to an Array: Difference between revisions

Content added Content deleted
(Added R Code)
(tag obsolete (Arrays) and remove {header}s)
Line 1: Line 1:
{{task|Basic language learning}}
{{task|Basic language learning}}
{{Template:clarify task}}
{{Template:clarify task}}

'''This task is obsolete. Please do not add new code, and move existing code to the [[Arrays]] task.'''


In this task, the goal is to assign a value to an element of an [[array]]. The value should only replace an existing value, and not insert a new key should the key not exist. If the key does not exist, an error should be returned.
In this task, the goal is to assign a value to an element of an [[array]]. The value should only replace an existing value, and not insert a new key should the key not exist. If the key does not exist, an error should be returned.


=={{header|ActionScript}}==
==[[ActionScript]]==
arr[0] = 1;
arr[0] = 1;
arr[1] = 'a';
arr[1] = 'a';
arr[2] = 5.678;
arr[2] = 5.678;


=={{header|Ada}}==
==[[Ada]]==
{{works with|GCC| 4.1.2}}
{{works with|GCC| 4.1.2}}
<lang ada>package Pkg is
<lang ada>package Pkg is
Line 23: Line 25:
end Pkg;</lang>
end Pkg;</lang>


=={{header|ALGOL 68}}==
==[[ALGOL 68]]==
Declarations:
Declarations:
FLEX [1:8] INT array:=(1,2,3,4,5,6,7,8);
FLEX [1:8] INT array:=(1,2,3,4,5,6,7,8);
Line 50: Line 52:
+333 +2 +33 +44 +55 +6 +7 +8 +333
+333 +2 +33 +44 +55 +6 +7 +8 +333


=={{header|APL}}==
==[[APL]]==


array[index]←value
array[index]←value
Line 56: Line 58:
APL is array-oriented, and so there are many different and powerful ways to index and assign arrays. Asking an APLer how one assigns a value to an array index is like asking an Eskimo how one says "snow". The above is just tip of the iceberg (i.e. a direct translation of the non-array-oriented code examples on this page).
APL is array-oriented, and so there are many different and powerful ways to index and assign arrays. Asking an APLer how one assigns a value to an array index is like asking an Eskimo how one says "snow". The above is just tip of the iceberg (i.e. a direct translation of the non-array-oriented code examples on this page).


=={{header|AppleScript}}==
==[[AppleScript]]==
set item location of array to value
set item location of array to value


Line 62: Line 64:
set end of array to item
set end of array to item


=={{header|AWK}}==
==[[AWK]]==
In the first example, a["hello"] is overwritten; in the second, a["hellx"] is not added.
In the first example, a["hello"] is overwritten; in the second, a["hellx"] is not added.
<lang awk>
<lang awk>
Line 76: Line 78:
awk: cmd. line:1: fatal: division by zero attempted</lang>
awk: cmd. line:1: fatal: division by zero attempted</lang>


=={{header|Brainf***}}==
==[[Brainf***]]==


To assign values 5, 6, and 7 to array elements 1,2,3:
To assign values 5, 6, and 7 to array elements 1,2,3:
Line 86: Line 88:
>+++++++ move value 7 to element 3
>+++++++ move value 7 to element 3


=={{header|C}}==
==[[C]]==
<lang c> #include <sys/types.h>
<lang c> #include <sys/types.h>
int writeToIntArray(int *array, size_t array_sz, int loc, int val)
int writeToIntArray(int *array, size_t array_sz, int loc, int val)
Line 97: Line 99:
}</lang>
}</lang>


=={{header|C sharp|C #}}==
==[[C sharp|C #]]==
'''Language Version:''' 1.0+
'''Language Version:''' 1.0+
<lang csharp>
<lang csharp>
Line 109: Line 111:
</lang>
</lang>


=={{header|C++}}==
==[[C++]]==
{{works with|g++| 4.1.2}}
{{works with|g++| 4.1.2}}
template<class T, std::size:t size>
template<class T, std::size:t size>
Line 127: Line 129:
}
}


=={{header|ColdFusion}}==
==[[ColdFusion]]==
<cffunction name="writeToArray">
<cffunction name="writeToArray">
<cfargument name="array">
<cfargument name="array">
Line 148: Line 150:
<cfset myArray[3] = 987654>
<cfset myArray[3] = 987654>


=={{header|Common Lisp}}==
==[[Common Lisp]]==


(setf (aref array index) value)
(setf (aref array index) value)


=={{header|D}}==
==[[D]]==


void setValue(T) (T[] array, size_t index, T newValue) { array[index] = newValue; }
void setValue(T) (T[] array, size_t index, T newValue) { array[index] = newValue; }


=={{header|E}}==
==[[E]]==


In E, you may expect that the FlexList data type is an array or something similarly efficient. However, a FlexList will implicitly extend itself if you give an index equal to the current size. Therefore, to implement the specified task:
In E, you may expect that the FlexList data type is an array or something similarly efficient. However, a FlexList will implicitly extend itself if you give an index equal to the current size. Therefore, to implement the specified task:
Line 170: Line 172:
The basic assignment operation, <code>a[b] := c</code>, is syntactic sugar; it is equivalent to <code>a.put(b, c)</code>, except that it returns <var>c</var> rather than the result of the call.
The basic assignment operation, <code>a[b] := c</code>, is syntactic sugar; it is equivalent to <code>a.put(b, c)</code>, except that it returns <var>c</var> rather than the result of the call.


=={{header|Delphi}}==
==[[Delphi]]==
procedure WriteToIntArray(var Arr: array of Integer; Loc: Integer; Val: Integer);
procedure WriteToIntArray(var Arr: array of Integer; Loc: Integer; Val: Integer);
begin
begin
Line 176: Line 178:
end;
end;


=={{header|Forth}}==
==[[Forth]]==
'''Interpreter:''' ANS Forth
'''Interpreter:''' ANS Forth
: ]! ( n addr ix -- ) cells + ! ; \ ex. 3 buffer[ 2 ]!
: ]! ( n addr ix -- ) cells + ! ; \ ex. 3 buffer[ 2 ]!


=={{header|Fortran}}==
==[[Fortran]]==
In ISO Fortran 90 or later, use an array initializer (with RESHAPE intrinsic for multidimensional arrays):
In ISO Fortran 90 or later, use an array initializer (with RESHAPE intrinsic for multidimensional arrays):
real, dimension(50) :: a = (/ (1.0/(i*i),i=1,50) /)
real, dimension(50) :: a = (/ (1.0/(i*i),i=1,50) /)
Line 196: Line 198:
a(55) = 12
a(55) = 12


=={{header|F_Sharp|F#}}==
==[[F_Sharp|F#]]==
let arr = [| 0; 1; 2; 3 |]
let arr = [| 0; 1; 2; 3 |]
arr.[0] <- 100
arr.[0] <- 100
Array.set arr 1 101 // alternative method
Array.set arr 1 101 // alternative method


=={{header|Groovy}}==
==[[Groovy]]==
Groovy, like every other C-derived language in the known universe, uses ZERO-based array/list indexing.
Groovy, like every other C-derived language in the known universe, uses ZERO-based array/list indexing.


Line 231: Line 233:
</pre>
</pre>


=={{header|Haskell}}==
==[[Haskell]]==
{{works with|GHC| 6.6}}
{{works with|GHC| 6.6}}
===List===
===List===
Line 294: Line 296:
</pre>
</pre>


=={{header|J}}==
==[[J]]==


array =: 5 5 5 5 5 5 5
array =: 5 5 5 5 5 5 5
Line 306: Line 308:
5 5 88 99 88 5 5
5 5 88 99 88 5 5


=={{header|Java}}==
==[[Java]]==
{{works with|J2SE| 1.2+}}
{{works with|J2SE| 1.2+}}
<lang java>public void writeToIntArray(int[] array, int loc, int val){
<lang java>public void writeToIntArray(int[] array, int loc, int val){
Line 322: Line 324:
}</lang>
}</lang>


=={{header|JavaScript}}==
==[[JavaScript]]==
function setElem(array, loc, val) { //returns 0 if out of bounds
function setElem(array, loc, val) { //returns 0 if out of bounds
if(typeof array[loc] == typeof undefined) {
if(typeof array[loc] == typeof undefined) {
Line 340: Line 342:
function writeToArray(array, loc, val) { array[loc] = val; }
function writeToArray(array, loc, val) { array[loc] = val; }


=={{header|Logo}}==
==[[Logo]]==
make "a {10 20 30}
make "a {10 20 30}
setitem 1 :a "ten
setitem 1 :a "ten


=={{header|LSE64}}==
==[[LSE64]]==
10 array :array
10 array :array
0 array 5 [] ! # store 0 at the sixth cell in the array
0 array 5 [] ! # store 0 at the sixth cell in the array


=={{header|mIRC Scripting Language}}==
==[[mIRC Scripting Language]]==
{{works with|mIRC|}}
{{works with|mIRC|}}
{{works with|mArray Snippet|}}
{{works with|mArray Snippet|}}
Line 354: Line 356:
alias write2array { echo -a $array_write(MyArray, 2, 3, Rosetta) }
alias write2array { echo -a $array_write(MyArray, 2, 3, Rosetta) }


=={{header|Mathematica}}==
==[[Mathematica]]==
Just like Part can extract parts of expressions, it can also be used to assign an element to a value:
Just like Part can extract parts of expressions, it can also be used to assign an element to a value:
<lang Mathematica>
<lang Mathematica>
Line 389: Line 391:
</lang>
</lang>


=={{header|MAXScript}}==
==[[MAXScript]]==
<pre>arr[index] = value</pre>
<pre>arr[index] = value</pre>
MAXScript arrays dynamically resize when assigning to an index that previously didn't exist.
MAXScript arrays dynamically resize when assigning to an index that previously didn't exist.


=={{header|Nial}}==
==[[Nial]]==
a := 1 2 3 4
a := 1 2 3 4
a@1 := 100
a@1 := 100
Line 399: Line 401:
=1 100 3 4
=1 100 3 4


=={{header|Objective-C}}==
==[[Objective-C]]==
{{works with|GCC| 3.3+}}
{{works with|GCC| 3.3+}}


Line 407: Line 409:
}
}


=={{header|OCaml}}==
==[[OCaml]]==
{{works with|OCaml|}}
{{works with|OCaml|}}
arr.(loc) <- val;
arr.(loc) <- val;
Line 413: Line 415:
Array.set arr loc val;
Array.set arr loc val;


=={{header|Perl}}==
==[[Perl]]==
{{works with|Perl|5.8.8}}
{{works with|Perl|5.8.8}}


Line 429: Line 431:
# will replace the 4th, 5th and 6th elements with the first 3 values in @values
# will replace the 4th, 5th and 6th elements with the first 3 values in @values


=={{header|PHP}}==
==[[PHP]]==


<?php
<?php
Line 456: Line 458:
?>
?>


=={{header|PL/SQL}}==
==[[PL/SQL]]==
'''Interpreter:''' Oracle compiler
'''Interpreter:''' Oracle compiler
set serveroutput on
set serveroutput on
Line 483: Line 485:
/
/


=={{header|Pop11}}==
==[[Pop11]]==


value -> array(index);
value -> array(index);
Line 490: Line 492:




=={{header|Python}}==
==[[Python]]==


To change existing item, (raise IndexError if the index does not exists):
To change existing item, (raise IndexError if the index does not exists):
Line 535: Line 537:




=={{header|R}}==
==[[R]]==
<lang R>
<lang R>
#Create array
#Create array
Line 543: Line 545:
</lang>
</lang>


=={{header|Ruby}}==
==[[Ruby]]==
{{works with|Ruby| 1.8.5}}
{{works with|Ruby| 1.8.5}}
# To specify a value for a known index
# To specify a value for a known index
Line 557: Line 559:
arr << 1
arr << 1


=={{header|Scala}}==
==[[Scala]]==
val array = new Array[int](10) // create a 10 element array of integers
val array = new Array[int](10) // create a 10 element array of integers
array(3) = 44
array(3) = 44
Line 575: Line 577:
l(2) // 2
l(2) // 2


=={{header|Scheme}}==
==[[Scheme]]==
(vector-set! arr loc val)
(vector-set! arr loc val)


=={{header|Slate}}==
==[[Slate]]==
<lang slate>
<lang slate>
Line 585: Line 587:
</lang>
</lang>


=={{header|Smalltalk}}==
==[[Smalltalk]]==


<lang smalltalk>"suppose array is bound to an array of 20 values"
<lang smalltalk>"suppose array is bound to an array of 20 values"
Line 594: Line 596:
do: [ :sig | 'Out of range!' displayNl ].</lang>
do: [ :sig | 'Out of range!' displayNl ].</lang>


=={{header|Standard ML}}==
==[[Standard ML]]==
Array.update (arr, loc, val);
Array.update (arr, loc, val);


=={{header|Tcl}}==
==[[Tcl]]==
<lang tcl>proc setIfExist {theVariable value} {
<lang tcl>proc setIfExist {theVariable value} {
upvar 1 $theVariable variable
upvar 1 $theVariable variable
Line 610: Line 612:
setIfExist x 10 ;# error if x doesn't exist</lang>
setIfExist x 10 ;# error if x doesn't exist</lang>


=={{header|Toka}}==
==[[Toka]]==
100 cells is-array foo
100 cells is-array foo
1000 10 foo array.put ( Put value '1000' into array 'foo' at slot '10' )
1000 10 foo array.put ( Put value '1000' into array 'foo' at slot '10' )
Line 616: Line 618:
char: A 1 foo array.putChar ( Put value 'A' (ASCII code) into character array 'bar' at slot '1' )
char: A 1 foo array.putChar ( Put value 'A' (ASCII code) into character array 'bar' at slot '1' )


=={{header|Visual Basic}}==
==[[Visual Basic]]==
'''Language Version:''' 5.0+
'''Language Version:''' 5.0+
Private Function writeToArray(intArray() As Integer, arraySize As Integer, loc As Integer, value As Integer) As Integer
Private Function writeToArray(intArray() As Integer, arraySize As Integer, loc As Integer, value As Integer) As Integer
Line 627: Line 629:
End Function
End Function


=={{header|VBScript}}==
==[[VBScript]]==


===Simple Example===
===Simple Example===