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

multi-dimensional tables

Started by westloar, 26 July 2015 - 03:34 PM
westloar #1
Posted 26 July 2015 - 05:34 PM
I am trying to create a multi-dimensional table and have the following code which returns the following error:



--code
cnx = {}
for i=1, 1, 60
cnx[i] = "open"
	    if i < 16 then
	    cnx[i][i] = "bank1"
	    end
end


--error
expecting index found string

I've looked online for some tutorials on multi-dimensional tables in lua and couldn't find much at all, any help would be appreciated

Cheers.
Lyqyd #2
Posted 26 July 2015 - 05:45 PM
If you set the value of cnx to the string "open", it is not a table, so you cannot use it as a table to try to then set the value of cnx.
westloar #3
Posted 26 July 2015 - 06:02 PM
It seems to work as a table as before trying to make it a dimensional table I had the following:


for i=1, 1,60 do
cnx[i] = "open"
end
for key,value inpairs(cnx) do
print(tostring(key)..tostring(value))
end

This returned keys 1-60 with "open" printed after.

I know very little about tables or lua in general, it appears to acting like a table, am I missing something?

Thanks for you help so far
Lyqyd #4
Posted 26 July 2015 - 06:08 PM
Right, cnx is a table. So when you put things into cnx in your loop, that works fine. But you don't create a table at cnx, so you can't put anything in to it. So cnx is trying to put something into a separate table that should be at cnx. But you didn't put a table there, you put a string there.
flaghacker #5
Posted 26 July 2015 - 06:17 PM
I'd suggest reading up on tables, for example on these pages:

tables:
http://lua-users.org/wiki/TablesTutorial

multi-dimentional tables:
http://www.lua.org/pil/11.2.html
westloar #6
Posted 26 July 2015 - 06:30 PM
Right, cnx is a table. So when you put things into cnx in your loop, that works fine. But you don't create a table at cnx, so you can't put anything in to it. So cnx is trying to put something into a separate table that should be at cnx. But you didn't put a table there, you put a string there.

Got ya, thanks!

Looks like I've got some readding up to do!