Migrate the data table:Modules?

I’m trying to migrate my data table to an external module. 

It’s working just fine as is but I will be adding a lot of data later on. 

So ideally i would like to separate the data table so I can just modify the data table without touching other elements in main.lua by mistake. 

 

I believe this is a job for a module (separate .lua file, as I understand) but I can’t figure out how to achieve this. There are some tutorials/ documentations but some of them say they are outdated. So I’m not sure where to start. 

 

Currently my data table looks something like this; 

 

local Data ={}

Data[1].name = “AAAA”

Data[1].detail = “BBBB”

Data[2].name = “CCCC”

Data[2].detail = “DDDD”

 

Data[3].name = “EEEE”

Data[3].detail = “FFFF”

 

 

And I would like to … 

-A ) migrate the Data={} data table to another .lua file

while  

-B ) the variable “Data” and individual data items are still accessible from main.lua 

Thaks!

Leo 

This should work:

-- data.lua local Data = {} Data[1] = {} Data[1].name = "AAAA" Data[1].detail = "BBBB" Data[2] = {} Data[2].name = "CCCC" Data[2].detail = "DDDD" Data[3] = {} Data[3].name = "EEEE" Data[3].detail = "FFFF" return Data -- main.lua data = require( "data" ) -- note: drop the ".lua" extension here print( data[1].name ) print( data[1].detail )

Another option would be to put the table into JSON-format (or XML), then read/parse that file into a lua table (this might make it easier if you want to change the data later, without having to rebuild the app).

Thank you Jedi. It worked like magic. 

This module method is perfectly fine for now. But eventually, I will be updating the data table frequently. So I will explore JSON format as well. 

This should work:

-- data.lua local Data = {} Data[1] = {} Data[1].name = "AAAA" Data[1].detail = "BBBB" Data[2] = {} Data[2].name = "CCCC" Data[2].detail = "DDDD" Data[3] = {} Data[3].name = "EEEE" Data[3].detail = "FFFF" return Data -- main.lua data = require( "data" ) -- note: drop the ".lua" extension here print( data[1].name ) print( data[1].detail )

Another option would be to put the table into JSON-format (or XML), then read/parse that file into a lua table (this might make it easier if you want to change the data later, without having to rebuild the app).

Thank you Jedi. It worked like magic. 

This module method is perfectly fine for now. But eventually, I will be updating the data table frequently. So I will explore JSON format as well.