This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
civilwargeeky's profile picture

[Question] Making a multi-dimensional array

Started by civilwargeeky, 03 November 2012 - 08:16 AM
civilwargeeky #1
Posted 03 November 2012 - 09:16 AM
So a part of a quarry program I'm making (somewhat) figures out what is in each slot and how many there are and stores them in a table (or it will) . So my question is how exactly do you make a multi-dimensional array, or a matrix. I already looked it up and don't understand why this
table = {}
table [1] = {}
table [1] = 1000
table [1][1] = 1
print(table[1])
print(table[1][1])
dosen't work, error on line 4: Index expected, got number. Could somebody explain how this works?

If its pretty complicated I could just use two separate tables.
jag #2
Posted 03 November 2012 - 09:40 AM
First: it's not the best idea to name your table "table", because theres a built in API named table.
Second: Do I get the question right? You want to get a table inside a table?
ruben654 #3
Posted 03 November 2012 - 09:42 AM
The problem is that you first assign 1000 to table[1], but you want to assign another table to it, and I also don't think calling a table table is a good idea.
Here are some examples of how to initialize a matrix:

matrix={{1,2,3},
			 {4,5,6},
			 {7,8,9}}
or
matrix={}
matrix[1]={} --you will have to do this for every row of the matrix
matrix[2]={}
matrix[3]={}

or even

matrix={{},{},{}}
You'll need to initialize every row like this, a column can have as many values as you want

if you've a matrix like this you can read or change it easily

print(matrix[x][y])
matrix[x][y]=value
civilwargeeky #4
Posted 03 November 2012 - 09:47 AM
Ok thank you. That makes more sense. Before I was thinking it worked differently. Now that you've explained it I don't really see what I was thinking.

And yeah, the table is not actually called table, that was just an example.
jag #5
Posted 03 November 2012 - 09:48 AM
So when you are doing this:


table = {}
table [1] = {}
table [1] = 1000
table [1][1] = 1
print(table[1])
print(table[1][1])

You first define the variable "table" as a table.
After this you define the first value of the table as a table.
Then you redefine the first value in the table as the number 1000.
After that you are setting the number 1000's first value to 1, as if it where a table.
So technically this is what you are doing:

table = {}
table = { [1] = {} }
table = { [1] = 1000 }
table = { [1] = { [1] = 1 } }
So what you want to do is:


table = {}
table [1] = {}
table [2] = 1000
table [1][1] = 1
print(table[2]) -- prints 1000
print(table[1][1]) -- 1

-- So now the table looks like this:
table = { [1] = { [1] = 1 }, [2] = 1000 }
-- or like this
table = { { 1 }, 1000 }
Lyqyd #6
Posted 03 November 2012 - 10:18 AM
No, technically he attempted to index a number, not redefine the value back from a number into a table.
kazagistar #7
Posted 04 November 2012 - 02:11 AM
Ok, let me handle this one. What you really want here is to create a default table. This means that when you access an item in the table, it will create a default value instead of nil.
-- First, you set up a metatable
-- When an unknown index is accessed, it will create a new table and return it
local matrixMetatable = {}
matrixMetatable.__index = function(self, index)
	local new = {}
	self[index] = new
	return new
end

-- Now create a table and make it use the metatable
local matrix = {}
setmetatable(matrix, matrixMetatable)

-- An example of how it is used
matrix[0][3] = "test"
matrix[2][7] = "more"
print(matrix[0][3])
print(matrix[2][7])
print(matrix[1][9])

You can also modify the metatable to set all sub-tables to use the metatable as well, which basically allows you to create arbitrary dimention matrixies or table "trees". More info about metatables.