简体   繁体   English

Lua 预期 = 接近数据

[英]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.从上面的代码可以看出,我的代码在变量data初始化的那一行抛出错误。 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.这也是我第一次使用 lua 编写代码,所以我没有使用这种语言的经验。

What could be wrong in this code that could cause this error此代码中可能有什么错误可能导致此错误

Global variables don't need to be declared explicitly like local ones do.全局变量不需要像local变量那样显式声明。 The interpreter is erroring because you are prefixing global to your variable, which isn't a recognized keyword解释器出错,因为您在变量前加上global前缀,这不是可识别的关键字

Try the following, without the global :在没有global的情况下尝试以下操作:

data = {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM