Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prefix a Lua table?

Tags:

lua

lua-table

I have a lua file whose content is lua Table as below: A={}, A.B={}, A.B.C=0;,

The problem is I want to add prefix XYZ before each above statements. So after the parse the database should have something loke this: XYZ.A={}, XYZ.A.B={}, XYZ.A.B.C={},

Any ideas? Thanks in advance

like image 377
Darshan Nair Avatar asked Dec 27 '22 00:12

Darshan Nair


2 Answers

You can load the file with XYZ as is environment: loadfile("mydata","t",XYZ). See loadfile in the manual.

This works in Lua 5.2. For Lua 5.1, use loadfile followed by setfenv.

like image 175
lhf Avatar answered Jan 15 '23 13:01

lhf


If you can afford polluting your global space with A, simply assign it later:

-- load the file
-- if XYZ doesn't exist, XYZ = { A = A } would be probably shorter
XYZ.A = A
A = nil
like image 28
Bartek Banachewicz Avatar answered Jan 15 '23 11:01

Bartek Banachewicz