user13524876
user13524876

Reputation:

Lua expected = near data

local file = 'data.txt'
local lines = lines_from(file)

global data = {} -- this line is giving the error "lua: day8.lua:20: '=' expected near 'data'"

for key, value in pairs(lines) do
  local row = {}
  for i=1, #value do
    local char = value:sub(i,i)
    row[i] = char
  end
  data[key] = row
end

As you can see from the code above, my code is throwing an error on the line where the variable data is initialised. This code was working earlier when I tested it, then I added more code below what is visible and it broke this line somehow.

I don't think its the code below that broke this line as otherwise why would it be showing there?

This is also my first ever code with lua so I have no experience with this language.

What could be wrong in this code that could cause this error

Upvotes: 0

Views: 152

Answers (1)

emirps
emirps

Reputation: 146

Global variables don't need to be declared explicitly like local ones do. The interpreter is erroring because you are prefixing global to your variable, which isn't a recognized keyword

Try the following, without the global:

data = {}

Upvotes: 3

Related Questions