How to merge tables

Sometimes it's useful to have a function able to merge two tables, this function is TB.Merge(). This function recursively add all missing items on the source table to the destination table, optionally it can overwrite existing items.

Here is the example provided with the library ecapsulated in the function TB.TEST_Merge().

  Local table1 = { name = "Fabio", surname = "Falcucci", age = 44 }
  Local table2 = { hobby = "Programming", nick = "Allanon", name = "What's up?" }
  
  NPrint("TABLE 1")
  ForEach(table1, NPrint)
  
  NPrint("\nTABLE 2")
  ForEach(table2, NPrint)
  
  Local result = TB.Merge(table1, table2, True)
  NPrint("\nTABLE 1")
  ForEach(table1, NPrint)
  
  NPrint("\nENTER TO QUIT")
  InKeyStr(#ALL)

The example does not need further explanations as you can see.

hollywoodlibs/tables/testmerge.txt · Ultima modifica: 2016/10/21 00:18 da admin
CC Attribution-Noncommercial 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0