hi i'm new with tables
1 | local testtable = { |
2 | subtable 1 = { "duck" , "hat" , "goose" } , |
3 | subtable 2 = { "truck," , "fart" , "house" } |
4 | } |
5 |
6 | for i, v in pairs (testtable) do |
7 | print (v [ "fart" ] ) |
8 | 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:
01 | local testtable = { |
02 | subtable 1 = { "duck" , "hat" , "goose" } , -- subtable1 is key, {"duck", "hat", "goose"} is its value |
03 | subtable 2 = { "truck" , "fart" , "house" } |
04 | } |
05 |
06 | for key, value in pairs (testtable) do |
07 | for i, v in pairs (value) do |
08 | if v = = "fart" then |
09 | print ( "Found fart!" ) |
10 | end |
11 | end |
12 | end |