小编PHa*_*zer的帖子

Lua:何时以及如何将表写入_G

我正在从书中学习Lua,而我不是程序员.我正在尝试使用以下函数(直接从本书中复制)将数据表保存到文件中,但是当尝试从_G [resTable]获取字符串时,该函数出错.为什么?

function readFromFile(filename,resTable)
    local hfile = io.open(filename)
    if hfile == nil then return end
    local results = {} -why is this table here?
    local a = 1
    for line in hfile:lines() do-- debug shows this loop doesn't run (no lines in hfile?)
        _G[resTable[a]] = line
        a = a + 1
    end
end

function writeToFile(filename, resTable)
    local hfile = io.open(filename, "w")
    if hfile == nil then return end
    local i
    for i=1, #resTable do
        hfile:write(_G[resTable[i]])--bad argument #1 to 'write' (string …
Run Code Online (Sandbox Code Playgroud)

lua global-variables lua-table

7
推荐指数
1
解决办法
1873
查看次数

标签 统计

global-variables ×1

lua ×1

lua-table ×1