toosweetnitemare
toosweetnitemare

Reputation: 2276

why is my global table being considered nil?

background:

I am trying to teach myself Lua and I am having difficulty understanding why a table is being considered nil when it has data inside it. Can anyone break it down for me why I am getting this error message from the code snippet below? It is one of my first programs and i really need to get these few concepts down before moving on to my real project. Thanks!

error message:

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]: ?

code:

--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

Upvotes: 0

Views: 181

Answers (1)

Mike Corcoran
Mike Corcoran

Reputation: 14564

you are missing the assignment to entry.

you need to change the entry code to this:

entry = 
{
    title = "test",
    org = "org",
    url = "http://www.google.com/",
    contact = "someone",
    description = [[
                    test1
                    test2
                    test3]]
}

to clarify the error message, parens are assumed in certain contexts, like when you have a table directly following a label. the interpreter thinks you're trying to pass a table to a function called entry, which it can't find. it assumes you really meant this:

entry({title = "test", ...})

Upvotes: 5

Related Questions