1

I have a table that holds gathered information from the user and populates in a format like this:

TempTable = {}
TempTable["date"] = {day, month, year}
TempTable["name"] = "Name Here"
TempTable["address"] = "Address Here" 
etc

I then have a main table that will hold all gathered information

MainTable = {}
MainTable[1] = { date = {day, month, year}, name = "Name Here", address = "Address Here" }
MainTable[2] = { date = {day, month, year}, name = "Name Here", address = "Address Here" }

Still pretty new to Lua so I'm having a bit of trouble copying the TempTable to the MainTable.

Sorry if this is a duplicate, I've checked many similar named threads before making this.

Edit: I've tried MainTable[i] = TempTable and table.insert(MainTable, TempTable). Problem is after doing this if any value in TempTable changes, it will also change in the MainTable.

NuMs
  • 139
  • 3
  • 15

2 Answers2

2
table.insert(MainTable, TempTable)

will help.

Please refer to: http://www.lua.org/manual/5.3/manual.html#pdf-table.insert

Of course you can also simply assign it to MainTable's elements like

MainTable[1] = TempTable

or MainTable[i] = TempTable in a loop...

Whatever you want.

Make sure you create a new table for every instance in MainTable. Tables are passed by reference. If you reuse or change TempTable you will also alter the contents of MainTable.

Simply create your TempTables in a small function (simplified example)

function CreateNewEntry(name)
 local newEntry = {}
 newEntry.name = name or "no name"
   return newEntry
end

Then use it like that:

 MainTable = {}
 table.insert(MainTable, CreateNewEntry("NuMs"))
 table.insert(MainTable, CreateNewEntry("Piglet"))
Piglet
  • 27,501
  • 3
  • 20
  • 43
  • 1
    Note that this passes a _reference_ of the table, so any value changed in `TempTable` will be changed in `MainTable[i]` – DavisDude Jun 01 '16 at 14:17
  • Problem is with both those methods is once I merge the TempTable into the MainTable the values in the MainTable will take any new values the TempTable takes. – NuMs Jun 01 '16 at 14:19
  • @DavisDude Heh, you posted that right as I posted my comment, that is exactly the problem. – NuMs Jun 01 '16 at 14:20
  • @NuMs of course I assumed you would create a new TempTable every time.. Sorry if I took that for granted. – Piglet Jun 01 '16 at 14:24
  • A new table every time results on smaller, cleaner, easier to understand code. When you call something "temp" you are on the wrong path. When you think that you want to assign a new value to a "variable" you're on the wrong path, .... (Of an iterator variable has a different value at each iteration but you don't do the assigning.) – Tom Blodget Jun 01 '16 at 17:13
1

As piglet pointed out, you can use temporarily tables, like he showed in his example. The reference to the table inside of the function will be lost after the function, so the only way to access newEntry is via the main table.

However, if you really need to copy (really copy) the table, you need to use a function for this. But keep in mind that you often don't have to copy.

In this answer, a very short deepcopy method for tables is shown. This can be used to make two tables independent of each other. This also handles some other stuff like metatables. Additionally, the answer gives some links to further readings.

I would suggest to you to make use of the penlight library for lua, a non-official 'standard' library which is very useful. It has a rich documentation including guides to show what you can use penlight for. In your case, it provides a deepcopy function, so it all comes down to this:

local tablex = require "pl.tablex"

local tempTable = {}
-- more code
local mainTable = {}
-- more code

table.insert(mainTable, tablex.deepcopy(tempTable))

At last, a little advice on lua syntax: A part of lua origins in a data description language, so it is possible to write such things down very easy and nice:

TempTable = {
    date = { day, month, year },
    name = "Name Here",
    address = "Address Here", -- you can put a comma AFTER the last element
}

Now using the function approach, you could pack everything up very nicely:

function create_entry(name, address, date)
    local date = date or get_current_date() -- use date parameter or the current date if absent
    return {
        name = name,
        address = address,
        date = date
    }
end

-- can be used like this:
table.insert(mainTable, create_entry("Foibudle", "12345 Dreamland", date("12-03-2015"))
-- can be used without the date:
table.insert(mainTable, create_entry("Foibudle", "12345 Dreamland")
Community
  • 1
  • 1
pschulz
  • 1,406
  • 13
  • 18