为什么我的全球表被认为是零?

too*_*are 0 printing null lua runtime-error lua-table

背景:

我正在努力教自己Lua,我很难理解为什么一个表在其中包含数据时被认为是nil.任何人都可以为我分解为什么我从下面的代码片段中收到此错误消息?这是我的第一个程序之一,我真的需要在继续我的真实项目之前先了解这几个概念.谢谢!

错误信息:

C:\Users\<user>\Desktop>lua luaCrap.lua
lua: luaCrap.lua:7: attempt to call global 'entry' (a nil value)
stack traceback:
        luaCrap.lua:7: in main chunk
        [C]: ?
Run Code Online (Sandbox Code Playgroud)

码:

--this creates the function to print
function fwrite (fmt, ...)
  return io.write(string.format(fmt, unpack(arg)))
end

--this is my table of strings to print
entry{
    title = "test",
    org = "org",
    url = "http://www.google.com/",
    contact = "someone",
    description = [[
                    test1
                    test2
                    test3]]
}

--this is to print the tables first value  
fwrite(entry[1])

--failed loop attempt to print table
-- for i = 1, #entry, 1 do
    -- local entryPrint = entry[i] or 'Fail'
    -- fwrite(entryPrint)
-- end
Run Code Online (Sandbox Code Playgroud)

Mik*_*ran 5

你错过了进入的任务.

您需要将输入代码更改为:

entry = 
{
    title = "test",
    org = "org",
    url = "http://www.google.com/",
    contact = "someone",
    description = [[
                    test1
                    test2
                    test3]]
}
Run Code Online (Sandbox Code Playgroud)

为了澄清错误消息,在某些上下文中假定了parens,就像在标签后面有一个表一样.解释器认为你试图将一个表传递给一个entry它找不到的函数.它假设你真的是这个意思:

entry({title = "test", ...})
Run Code Online (Sandbox Code Playgroud)