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

[Lua][Question] How to access table values with input variable?

Started by ndm250, 02 November 2012 - 08:22 AM
ndm250 #1
Posted 02 November 2012 - 09:22 AM
So I have a table defined as


itemTable = {}

that contains various blocks such as


itemTable.cobblestone = {}

and that table contains info about the block, like the id.

itemTable.cobblestone.id = 4

Now say if I wanted to retrieve the id of a block when using the input from the user


item = os.read()

How do I use that variable to access the id (if item = cobblestone)?


print(itemTable.item.id)

Lua treats the "item" segment as a literal table not as a variable.

Any suggestions?
KaoS #2
Posted 02 November 2012 - 09:34 AM
itemTable[item].id will work just fine
Kolpa #3
Posted 02 November 2012 - 09:35 AM
there is this way to handle tabels

itemTabel["cobblestone"]["id"]

also there is a combination of them

itemTable[item].id
that should to the trick
KaoS #4
Posted 02 November 2012 - 09:39 AM
YESSS. for once I was not ninja'd :D/>/>
Kolpa #5
Posted 02 November 2012 - 09:43 AM
but i was T-T
KaoS #6
Posted 02 November 2012 - 09:52 AM

many profuse apologies there. It's quite irritating isn't it?