For example,
local dictionary = {["cow"] = 1, ["sheep"] = 1} local random = dictionary[math.random(1, #dictionary)] print(random) --This would print 1
but how do I get the name of where the 1 came from?
local dictionary = {["cow"] = 1, ["sheep"] = 1} local random = dictionary[math.random(1, #dictionary)] print(????) --This would print where the 1 came from
What you can do is loop
through the table
, and check for the value
of the key
, then you will be able to print
the key.
Something like this:
local table1 = {} table1["cow"] = 1 for i, v in pairs(table1) do if v == 1 then print(i) end end
Hope I helped, if you have any further questions please let me know.
The correct name of this post should be : How to get "cow" out of a table and print it?. But its fine. Here you go!
local table = {} table["cow"] = "Cow" print(table["cow"])
It seems you had it backwards. Using table["Cow"] = 1 will be like this in the table ["Cow"] = {1}