How to prefix a Lua table?

I have a lua file whose contents is lua. The table below: A={} AB={} ABC=0; ,

The problem is that I want to add the XYZ prefix before each statement above. Therefore, after parsing the database should be something like this: XYZ.A={} XYZ.AB={} XYZ.ABC={} ,

Any ideas? thanks in advance

+4
source share
4 answers

You can upload a file using XYZ as the medium: loadfile("mydata","t",XYZ) . See loadfile in the manual.

This works in Lua 5.2. For Lua 5.1, use loadfile and then setfenv .

+4
source

If you can afford to pollute your global space A , just assign it later:

 -- load the file -- if XYZ doesn't exist, XYZ = { A = A } would be probably shorter XYZ.A = A A = nil 
+3
source

I think this is what you want:

 XYZ = {} XYZ.A = {} XYZ.AB = {} XYZ.ABC = 0 
+2
source

What about what you just do:

 XYZ = { A = { B = { C = 0 } } } 

If you don’t want to embed objects so deep, you can:

 XYZ = { A = A } A = nil 

It is assumed that you have already declared object A before.

+1
source

Source: https://habr.com/ru/post/1490260/


All Articles