Assigning Values to an Array: Difference between revisions

rm JavaScript and LSE
(rm JavaScript and LSE)
Line 319:
map.put(key,val);
}</lang>
 
==[[JavaScript]]==
function setElem(array, loc, val) { //returns 0 if out of bounds
if(typeof array[loc] == typeof undefined) {
return 0; //element doesn't already exist -- out of bounds
} else {
array[loc] = val
return 1; //OK
}//if
}//setElem
//use:
var ary=[10,20,30] //0,1,2 defined
var ok = setElem(ary,3,'three')
if(!ok) alert('oops, error')
 
Simpler if you don't mind adding an element if it does not already exist:
function writeToArray(array, loc, val) { array[loc] = val; }
 
==[[LSE64]]==
10 array :array
0 array 5 [] ! # store 0 at the sixth cell in the array
 
==[[mIRC Scripting Language]]==
Anonymous user