不同模块文件中相同的变量名始终返回最后一个。

huangapple go评论68阅读模式
英文:

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.ChaptertwoModule.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.

huangapple
  • 本文由 发表于 2020年1月7日 01:34:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定