How to play with a function that returns a table in lua?

I cannot process the table that the function returns. Can someone help me with this?

local grades = { Mary = "100", Teacher="100",'4','6'}
print "Printing grades!"
grades.joe = "10"
grades_copy = grades
for k, v in ipairs(grades) do
  --  print "Grade:"
   -- print(k, v)
end
function returntable()
    tablein = grades
    return 'hello'
end

grades_copy_table = returntable
--print(grades_copy_table)

In this program above, I want to access table elements through this "returntable" function, which returns a table.

+4
source share
1 answer

In Lua, functions are first-class values.

grades_copy_table = returntable

Here you are assigning a grades_copy_tablefunction returntable, not its return value. You need to call this function and assign a return value:

grades_copy_table = returntable()
+4
source

All Articles