hi i'm new with tables
local testtable = { subtable1 = {"duck","hat","goose"}, subtable2 = {"truck,","fart","house"} } for i, v in pairs(testtable) do print(v["fart"]) end
it prints nil(x2)
When you put v["fart"]
, you're attempting to check if the index "fart" is within the testtable
. That would be the case for a dictionary, so print(testtable["subtable1"])
would work since subtable1
is the key in your dictionary. You have the right idea on using a for loop, but then you'll need another for loop to access the nested tables! Here's how:
local testtable = { subtable1 = {"duck", "hat", "goose"}, -- subtable1 is key, {"duck", "hat", "goose"} is its value subtable2 = {"truck", "fart", "house"} } for key, value in pairs(testtable) do for i, v in pairs(value) do if v == "fart" then print("Found fart!") end end end