[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Lua file call
- From: Thijs Schreijer <thijs@...>
- Date: Fri, 7 Nov 2014 08:03:21 +0000
> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of Zulfihar Avuliya
> Sent: vrijdag 7 november 2014 8:44
> To: lua-l@lists.lua.org
> Subject: Lua file call
>
> Hello Team,
>
> I am new to lua and I have question regarding how to call a lua script from
> another lua script:
>
> Example :
> File 1: Master.Lua
> table2 = {
> P1 = 1,
> P2 = 0,
> P3 = 0,
> P4 = 1,
> P5 = 0,
> P6 = 0,
> }
> .....
>
>
> File 2 : Fileopen.lua
>
> table3 = {
> Value1 = 1
> Value2 = 5
> Value3 = 4
> Value4 = 3
> }
NOTE: invalid code, you'll need ',' delimiters
>
> 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
>
There are multiple ways of doing this. From simple to not-so-simple.
1) use 'require()' (see manual for details)
Make sure the second file is in your LUA_PATH environment, and end the file with line
> return table3
Or replace the initial 'table3 = {' line with
> return {
In your main file you can now do;
> local table3 = require("Fileopen")
2) load the file manually in its own environment
Loaded code is run as a function, if you provide it its own environment table, then you don't need the delimiters as mentioned above, and it has some minimal sandboxing.
See function 'load()' or 'loadfile()' in the manual
Use this; File 2 : Fileopen.lua
Value1 = 1
Value2 = 5
Value3 = 4
Value4 = 3
Load it using;
> local table3=loadfile("Fileopen.lua", nil, {})
A key question here is; is the file you're opening trusted or not? If not, you'll need a better sandboxed environment.
Hth
Thijs