Extensions Set

void array:set ( number i1, i2, ... in, value )

Stores a new value at the calculated n-dimensional index given by i1, i2, ... in.  The number of indexes must match the dimensionality originally specified in array.new() or the last call to resize().  There is no bounds checking provided on the individual indexes.  The value parameter must be the last parameter, after the variable number of index parameters.

Given dimensions of size d1, d2, ... dn, and indexes i1, i2, ... in, then the index is calculated as:
idx = [(d2 * d3 * ... * dn) * i1] + [(d3 * d4 * ... * dn) * i2] + ... + (dn * in-1) + in

For a 3-dimensional array then, the calculated index would be:
idx = [(d2 * d3) * i1] + (d3 * i2) + i3

Parameters

in One or more array indexes matching the dimensionality of the array 
value The value to assign to the array at the calculated index

Return Values

None.

Example

-- Assume we have a 3-dimensional array "arr"
arr:set(1, 2, 3, 80)
 
-- If the array was created with dimensions 4, 5, 6
-- then the following value should be the same as the
-- value we just stored
local idx = (5 * 6 * 1) + (6 * 2) + 3
local val = arr[idx]
 
assert(val == 80)

Copyright 2010 Justin Aquadro