Module 不同模块文件中的相同变量名始终返回最后一个变量名
我在每个模块中编写了两个Module 不同模块文件中的相同变量名始终返回最后一个变量名,module,lua,Module,Lua,我在每个模块中编写了两个lua模块,每个模块都有相同的变量名chapter,但字符串不同。在主代码中,当我尝试打印所有章节时,即我将从不同的模块中获取章节并全部打印,只有最后加载的模块才能打印其章节 如何访问主代码中每个模块中的章节变量?以下是MWE: 第一个模块: local modOne = {} Chapter = {} Chapter[1] = {chapNum = 1} Chapter[1][1] = "This is the first verse of t
lua
模块,每个模块都有相同的变量名chapter
,但字符串不同。在主代码中,当我尝试打印所有章节时,即我将从不同的模块中获取章节并全部打印,只有最后加载的模块才能打印其章节
如何访问主代码中每个模块中的章节变量?以下是MWE:
第一个模块:
local modOne = {}
Chapter = {}
Chapter[1] = {chapNum = 1}
Chapter[1][1] = "This is the first verse of the modOne"
Chapter[1][2] = "This is the second verse of the modOne"
Chapter[2] = {chapNum = 2}
Chapter[2][1] = "This is the third verse of the modOne"
Chapter[2][2] = "This is the fourth verse of the modOne"
return modOne
local modTwo = {}
Chapter = {}
Chapter[1] = {chapNum = 1}
Chapter[1][1] = "This is the first verse of the modTwo"
Chapter[1][2] = "This is the second verse of the modTwo"
Chapter[2] = {chapNum = 2}
Chapter[2][1] = "This is the third verse of the modTwo"
Chapter[2][2] = "This is the fourth verse of the modTwo"
return modTwo
oneModule = require('modOne')
twoModule = require('modTwo')
for i = 1, #Chapter do
for j = 1, #Chapter[i] do
print(Chapter[i][j])
end
end
第二模块:
local modOne = {}
Chapter = {}
Chapter[1] = {chapNum = 1}
Chapter[1][1] = "This is the first verse of the modOne"
Chapter[1][2] = "This is the second verse of the modOne"
Chapter[2] = {chapNum = 2}
Chapter[2][1] = "This is the third verse of the modOne"
Chapter[2][2] = "This is the fourth verse of the modOne"
return modOne
local modTwo = {}
Chapter = {}
Chapter[1] = {chapNum = 1}
Chapter[1][1] = "This is the first verse of the modTwo"
Chapter[1][2] = "This is the second verse of the modTwo"
Chapter[2] = {chapNum = 2}
Chapter[2][1] = "This is the third verse of the modTwo"
Chapter[2][2] = "This is the fourth verse of the modTwo"
return modTwo
oneModule = require('modOne')
twoModule = require('modTwo')
for i = 1, #Chapter do
for j = 1, #Chapter[i] do
print(Chapter[i][j])
end
end
主代码:
local modOne = {}
Chapter = {}
Chapter[1] = {chapNum = 1}
Chapter[1][1] = "This is the first verse of the modOne"
Chapter[1][2] = "This is the second verse of the modOne"
Chapter[2] = {chapNum = 2}
Chapter[2][1] = "This is the third verse of the modOne"
Chapter[2][2] = "This is the fourth verse of the modOne"
return modOne
local modTwo = {}
Chapter = {}
Chapter[1] = {chapNum = 1}
Chapter[1][1] = "This is the first verse of the modTwo"
Chapter[1][2] = "This is the second verse of the modTwo"
Chapter[2] = {chapNum = 2}
Chapter[2][1] = "This is the third verse of the modTwo"
Chapter[2][2] = "This is the fourth verse of the modTwo"
return modTwo
oneModule = require('modOne')
twoModule = require('modTwo')
for i = 1, #Chapter do
for j = 1, #Chapter[i] do
print(Chapter[i][j])
end
end
代码总是读取上次加载的模块中的
章节
变量,但我想选择要打印的章节
。例如,我试图通过oneModule.Chapter[1][1]
或twoModule.Chapter[2][1]
访问每个模块中的Chapter
变量,但它返回了一个错误。对您提供的示例模块进行了编码,以便不会向返回的表中添加任何内容
这导致章
是一个全局变量,由第一个模块创建,然后由第二个模块更改
要纠正这一点,模块的编写应如下所示:
莫通:
模式二:
主要代码:
当您在模块中定义
章节
时,您也可以简单地确保将modOne.Chapter
放在模块中的任何地方。非常感谢!不要忘记用逗号分隔章节
条目。