Jump to content

Assigning Values to an Array: Difference between revisions

tag obsolete (Arrays) and remove {header}s
(Added R Code)
(tag obsolete (Arrays) and remove {header}s)
Line 1:
{{task|Basic language learning}}
{{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.
 
=={{header|[[ActionScript}}]]==
arr[0] = 1;
arr[1] = 'a';
arr[2] = 5.678;
 
=={{header|[[Ada}}]]==
{{works with|GCC| 4.1.2}}
<lang ada>package Pkg is
Line 23 ⟶ 25:
end Pkg;</lang>
 
=={{header|[[ALGOL 68}}]]==
Declarations:
FLEX [1:8] INT array:=(1,2,3,4,5,6,7,8);
Line 50 ⟶ 52:
+333 +2 +33 +44 +55 +6 +7 +8 +333
 
=={{header|[[APL}}]]==
 
array[index]←value
Line 56 ⟶ 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).
 
=={{header|[[AppleScript}}]]==
set item location of array to value
 
Line 62 ⟶ 64:
set end of array to item
 
=={{header|[[AWK}}]]==
In the first example, a["hello"] is overwritten; in the second, a["hellx"] is not added.
<lang awk>
Line 76 ⟶ 78:
awk: cmd. line:1: fatal: division by zero attempted</lang>
 
=={{header|[[Brainf***}}]]==
 
To assign values 5, 6, and 7 to array elements 1,2,3:
Line 86 ⟶ 88:
>+++++++ move value 7 to element 3
 
=={{header|[[C}}]]==
<lang c> #include <sys/types.h>
int writeToIntArray(int *array, size_t array_sz, int loc, int val)
Line 97 ⟶ 99:
}</lang>
 
=={{header|[[C sharp|C #}}]]==
'''Language Version:''' 1.0+
<lang csharp>
Line 109 ⟶ 111:
</lang>
 
=={{header|[[C++}}]]==
{{works with|g++| 4.1.2}}
template<class T, std::size:t size>
Line 127 ⟶ 129:
}
 
=={{header|[[ColdFusion}}]]==
<cffunction name="writeToArray">
<cfargument name="array">
Line 148 ⟶ 150:
<cfset myArray[3] = 987654>
 
=={{header|[[Common Lisp}}]]==
 
(setf (aref array index) value)
 
=={{header|[[D}}]]==
 
void setValue(T) (T[] array, size_t index, T newValue) { array[index] = newValue; }
 
=={{header|[[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:
Line 170 ⟶ 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.
 
=={{header|[[Delphi}}]]==
procedure WriteToIntArray(var Arr: array of Integer; Loc: Integer; Val: Integer);
begin
Line 176 ⟶ 178:
end;
 
=={{header|[[Forth}}]]==
'''Interpreter:''' ANS Forth
: ]! ( n addr ix -- ) cells + ! ; \ ex. 3 buffer[ 2 ]!
 
=={{header|[[Fortran}}]]==
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) /)
Line 196 ⟶ 198:
a(55) = 12
 
=={{header|[[F_Sharp|F#}}]]==
let arr = [| 0; 1; 2; 3 |]
arr.[0] <- 100
Array.set arr 1 101 // alternative method
 
=={{header|[[Groovy}}]]==
Groovy, like every other C-derived language in the known universe, uses ZERO-based array/list indexing.
 
Line 231 ⟶ 233:
</pre>
 
=={{header|[[Haskell}}]]==
{{works with|GHC| 6.6}}
===List===
Line 294 ⟶ 296:
</pre>
 
=={{header|[[J}}]]==
 
array =: 5 5 5 5 5 5 5
Line 306 ⟶ 308:
5 5 88 99 88 5 5
 
=={{header|[[Java}}]]==
{{works with|J2SE| 1.2+}}
<lang java>public void writeToIntArray(int[] array, int loc, int val){
Line 322 ⟶ 324:
}</lang>
 
=={{header|[[JavaScript}}]]==
function setElem(array, loc, val) { //returns 0 if out of bounds
if(typeof array[loc] == typeof undefined) {
Line 340 ⟶ 342:
function writeToArray(array, loc, val) { array[loc] = val; }
 
=={{header|[[Logo}}]]==
make "a {10 20 30}
setitem 1 :a "ten
 
=={{header|[[LSE64}}]]==
10 array :array
0 array 5 [] ! # store 0 at the sixth cell in the array
 
=={{header|[[mIRC Scripting Language}}]]==
{{works with|mIRC|}}
{{works with|mArray Snippet|}}
Line 354 ⟶ 356:
alias write2array { echo -a $array_write(MyArray, 2, 3, Rosetta) }
 
=={{header|[[Mathematica}}]]==
Just like Part can extract parts of expressions, it can also be used to assign an element to a value:
<lang Mathematica>
Line 389 ⟶ 391:
</lang>
 
=={{header|[[MAXScript}}]]==
<pre>arr[index] = value</pre>
MAXScript arrays dynamically resize when assigning to an index that previously didn't exist.
 
=={{header|[[Nial}}]]==
a := 1 2 3 4
a@1 := 100
Line 399 ⟶ 401:
=1 100 3 4
 
=={{header|[[Objective-C}}]]==
{{works with|GCC| 3.3+}}
 
Line 407 ⟶ 409:
}
 
=={{header|[[OCaml}}]]==
{{works with|OCaml|}}
arr.(loc) <- val;
Line 413 ⟶ 415:
Array.set arr loc val;
 
=={{header|[[Perl}}]]==
{{works with|Perl|5.8.8}}
 
Line 429 ⟶ 431:
# will replace the 4th, 5th and 6th elements with the first 3 values in @values
 
=={{header|[[PHP}}]]==
 
<?php
Line 456 ⟶ 458:
?>
 
=={{header|[[PL/SQL}}]]==
'''Interpreter:''' Oracle compiler
set serveroutput on
Line 483 ⟶ 485:
/
 
=={{header|[[Pop11}}]]==
 
value -> array(index);
Line 490 ⟶ 492:
 
 
=={{header|[[Python}}]]==
 
To change existing item, (raise IndexError if the index does not exists):
Line 535 ⟶ 537:
 
 
=={{header|[[R}}]]==
<lang R>
#Create array
Line 543 ⟶ 545:
</lang>
 
=={{header|[[Ruby}}]]==
{{works with|Ruby| 1.8.5}}
# To specify a value for a known index
Line 557 ⟶ 559:
arr << 1
 
=={{header|[[Scala}}]]==
val array = new Array[int](10) // create a 10 element array of integers
array(3) = 44
Line 575 ⟶ 577:
l(2) // 2
 
=={{header|[[Scheme}}]]==
(vector-set! arr loc val)
 
=={{header|[[Slate}}]]==
<lang slate>
Line 585 ⟶ 587:
</lang>
 
=={{header|[[Smalltalk}}]]==
 
<lang smalltalk>"suppose array is bound to an array of 20 values"
Line 594 ⟶ 596:
do: [ :sig | 'Out of range!' displayNl ].</lang>
 
=={{header|[[Standard ML}}]]==
Array.update (arr, loc, val);
 
=={{header|[[Tcl}}]]==
<lang tcl>proc setIfExist {theVariable value} {
upvar 1 $theVariable variable
Line 610 ⟶ 612:
setIfExist x 10 ;# error if x doesn't exist</lang>
 
=={{header|[[Toka}}]]==
100 cells is-array foo
1000 10 foo array.put ( Put value '1000' into array 'foo' at slot '10' )
Line 616 ⟶ 618:
char: A 1 foo array.putChar ( Put value 'A' (ASCII code) into character array 'bar' at slot '1' )
 
=={{header|[[Visual Basic}}]]==
'''Language Version:''' 5.0+
Private Function writeToArray(intArray() As Integer, arraySize As Integer, loc As Integer, value As Integer) As Integer
Line 627 ⟶ 629:
End Function
 
=={{header|[[VBScript}}]]==
 
===Simple Example===
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.