Array

From Mario Fan Games Galaxy Wiki
Data Types
Array

Arrays are a data structure available in many languages, useful for storing a list of values that can be accessed with a numeric index. Arrays using string keys instead of indexes are more often specified as associative arrays.

A value can be accessed with array[0], generally. In some higher-level languages, arrays are able to store multiple data types (mixing types such as numbers, strings, and functions), as opposed to just one. Arrays can sometimes be multi-dimensional, with the extra dimensions usually accessed like array[3][4][6], however, care should be taken with these as they can use a lot of memory.

Depending on the language, arrays may be static or dynamic. The former is faster and easier to implement, but the latter has the ability to grow and shrink during runtime.

Arrays can usually be traversed with a special for loop or even a dedicated loop. This is useful for various things, such as copying parts of one array to another, or to a file. The following code examples show how to iterate through an array and print its contents:

Lua

-- in Lua, an array is the most basic form of a table
array = {10, 5, 3, 6, "bacon", true, true, false, function () return 5 end}

-- using the 'ipairs' iterator
for i, v in ipairs (array) do 
    print(i, v)
end

--[[ prints:
1	10
2	5
3	3
4	6
5	bacon
6	true
7	true
8	false
9	function: 0x8070d58
--]]
-- note: the function return is a memory address and will vary

Python