Getting the value of a table in a table

Viewed 343

I'm very new to coding and stuck on a problem with tables. I'm trying to code a Minecraft turtle form the Computercraft mod to check the state of wheat. I need to find the age number that is in a table inside of a table.

This is what I have.

--inspect test
local success data = turtle.inspectDown() --gets the first table

if (success) then 
  print("block inspected")
  for k,v in pairs(data) do
     print(k, ": ", v)
  end
else
  print(data)
end
Output: 
state :  table: 1e2bb4c
name :  minecraft:wheat
tags :  table: 24b41937

I want to print the tables state and tags to see what is in them but I cant figure out how to do it.

1 Answers

You can use type to check if v is a table and then print the contents.

data = {
  state ={
    some = "stateData"
  },
  name = "minecraft:wheat",
  tag ={
    some = "tagData"
  },
}

for k,v in pairs(data) do
    print(k, ": ", v)
    if type(v) == "table" then 
        for l,u in pairs(v) do
            print("",l, ": ", u)
        end 
    end
end

Output:

state   :   table: 0x00721c40
    someKey :   stateData
name    :   minecraft:wheat
tag :   table: 0x00727fe8
    someKey :   tagData
Related