Jump to content




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


5 replies to this topic

#1 ndm250

  • Members
  • 6 posts

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?

#2 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 02 November 2012 - 09:34 AM

itemTable[item].id will work just fine

#3 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

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

#4 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 02 November 2012 - 09:39 AM

YESSS. for once I was not ninja'd :D/>

#5 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 02 November 2012 - 09:43 AM

View PostKaoS, on 02 November 2012 - 09:39 AM, said:

but i was T-T

#6 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 02 November 2012 - 09:52 AM

View PostKolpa, on 02 November 2012 - 09:43 AM, said:

View PostKaoS, on 02 November 2012 - 09:39 AM, said:

but i was T-T

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users