英文:
Same variable name in different module files return always the last one
问题
我在两个lua
模块中都写了一个名为chapter
的变量,但它们包含不同的字符串。在主代码中,当我尝试打印所有章节时,即从不同的模块获取它们并打印它们时,只有最后加载的模块的章节被打印出来。
如何在主代码中访问每个模块中的章节变量?以下是最小可行示例(MWE):
第一个模块:
local modOne = {}
Chapter = {}
Chapter[1] = {chapNum = 1}
Chapter[1][1] = "This is the first verse of modOne"
Chapter[1][2] = "This is the second verse of modOne"
Chapter[2] = {chapNum = 2}
Chapter[2][1] = "This is the third verse of modOne"
Chapter[2][2] = "This is the fourth verse of modOne"
return modOne
第二个模块:
local modTwo = {}
Chapter = {}
Chapter[1] = {chapNum = 1}
Chapter[1][1] = "This is the first verse of modTwo"
Chapter[1][2] = "This is the second verse of modTwo"
Chapter[2] = {chapNum = 2}
Chapter[2][1] = "This is the third verse of modTwo"
Chapter[2][2] = "This is the fourth verse of modTwo"
return modTwo
主代码:
oneModule = require('modOne')
twoModule = require('modTwo')
for i = 1, #oneModule.Chapter do
for j = 1, #oneModule.Chapter[i] do
print(oneModule.Chapter[i][j])
end
end
for i = 1, #twoModule.Chapter do
for j = 1, #twoModule.Chapter[i] do
print(twoModule.Chapter[i][j])
end
end
在主代码中,您可以通过oneModule.Chapter
和twoModule.Chapter
来分别访问两个模块中的Chapter
变量的内容。这将允许您打印出每个模块中的章节内容。
英文:
I wrote two lua
modules with the the same variable name chapter
in each one of them, but with different strings. In the main code, when I try to print all the chapters, i.e. I would get the chapters from the different modules and printed them all, only the last loaded modules get its chapters printed.
How do I access the chapters variables in each module in the main code? Here's the MWE:
First module:
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
Second module:
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
Main code:
oneModule = require('modOne')
twoModule = require('modTwo')
for i = 1, #Chapter do
for j = 1, #Chapter[i] do
print(Chapter[i][j])
end
end
The code reads always the Chapter
variable that is in the last module loaded, but I would like to choose which Chapter
I want to print. I tried to access the Chapter
variable in each module by oneModule.Chapter[1][1]
or twoModule.Chapter[2][1]
for example, but it returns an error.
答案1
得分: 3
The example modules you provide are coded so that nothing is added to the table that is returned.
This results in Chapter
being a global variable, which is created by the first module and then changed by the second.
To correct this, the modules should be written like:
modOne:
local modOne = {
Chapter = {
[1] = {
chapNum = 1,
[1] = "This is the first verse of the modOne",
[2] = "This is the second verse of the modOne",
},
[2] = {
chapNum = 2,
[1] = "This is the third verse of the modOne",
[2] = "This is the fourth verse of the modOne",
}
}
}
return modOne
modTwo:
local modTwo = {
Chapter = {
[1] = {
chapNum = 1,
[1] = "This is the first verse of the modTwo",
[2] = "This is the second verse of the modTwo",
},
[2] = {
chapNum = 2,
[1] = "This is the third verse of the modTwo",
[2] = "This is the fourth verse of the modTwo",
}
}
}
return modTwo
Main Code:
oneModule = require('modOne')
twoModule = require('modTwo')
for i = 1, #oneModule.Chapter do
for j = 1, #oneModule.Chapter[i] do
print(oneModule.Chapter[i][j])
end
end
for i = 1, #twoModule.Chapter do
for j = 1, #twoModule.Chapter[i] do
print(twoModule.Chapter[i][j])
end
end
You could also simply be sure to put modOne.Chapter
when you define Chapter
in the module and everywhere you use it within the module.
英文:
The example modules you provide are coded so that nothing is added to the table that is returned.
This results in Chapter
being a global variable, which is created by the first module and then changed by the second.
To correct this the modules should be written like:
modOne:
local modOne = {
Chapter = {
[1] = {
chapNum = 1,
[1] = "This is the first verse of the modOne",
[2] = "This is the second verse of the modOne",
},
[2] = {
chapNum = 2,
[1] = "This is the third verse of the modOne",
[2] = "This is the fourth verse of the modOne",
}
}
}
return modOne
modTwo:
local modTwo = {
Chapter = {
[1] = {
chapNum = 1,
[1] = "This is the first verse of the modTwo",
[2] = "This is the second verse of the modTwo",
},
[2] = {
chapNum = 2,
[1] = "This is the third verse of the modTwo",
[2] = "This is the fourth verse of the modTwo",
}
}
}
return modTwo
Main Code:
oneModule = require('modOne')
twoModule = require('modTwo')
for i = 1, #oneModule.Chapter do
for j = 1, #oneModule.Chapter[i] do
print(oneModule.Chapter[i][j])
end
end
for i = 1, #twoModule.Chapter do
for j = 1, #twoModule.Chapter[i] do
print(twoModule.Chapter[i][j])
end
end
You could also simply be sure to put modOne.Chapter
when you define Chapter
in the module and everywhere you use it within the module.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论