lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On Nov 6, 2014, at 11:43 PM, Zulfihar Avuliya <zulfihar.05@hotmail.co.uk> wrote:

Hello Team,

I want to read the values of table 3 from File 1: Master.lua, like this :
print(table3["Value1"])
Can you help me how to solve if this possible !

Thanks in advance:)

Zulfihar 

Study the load() API. The basic thing to understand is that when load() handles a “chunk” it always treats it as though it were a function. So in your examples Lua effectively wraps the code in “function(…) <your code> end” before compiling it.

What this means, is that when you load one of your files, what you get back from load() is a function (just like any other). When you then *execute* that function, it does whatever was in your file, which in the case of fileopen.lua will do a assignment to the global “table3”. For example:

f = load(…..)
f()
print(table3.Value1)

In fact, you can avoid having to declare the global names in the Lua files by changing the assignment to a return statement:

return {
Value1 = 1
}

Then, when you execute the compiled function, it will just return the table to your code, like this:

f = load(….)
table3 = f()
print(table3.Value1)

As others have noted, be careful when loading external files; if you don’t have control over the file contents, using load() can inject malicious code into your script.

HTH
—Tim