lua hash table code example

Example 1: lua tables

local favoritefoods_table = {"hamburger", "spaghetti", "pizza", "potato chips"}
--the table--

for i, v in pairs(favoritefoods_table) do --loop through the table-- 
	print(i) --print the number--
  	print(v) --print the value--
end

Example 2: how to make a table in lua

--You need to use this {} and put them in a varible
local Table = {13,"1hihihi",
-- u can even do tis
{122,222}}

Example 3: lua hash table length

-- there is no way to achieve this other than to create your own function.
-- using #table ("#" is shorthand for table.getn(table)) won't factor in key names.
function table_length(t)
    local z = 0
    for i,v in pairs(t) do z = z + 1 end
    return z
end

Tags:

Lua Example