Extensions At

number array:at ( number i1, i2, ... in )

Returns the 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 

Return Values

number Value at the calculated index 

Example

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

Copyright 2010 Justin Aquadro